Explain backpressure in Kotlin Flows and its management operators

This tests your understanding of Flow's sequential nature and strategies for decoupling producers and consumers. Explain default suspension, then detail buffer(), conflate(), and collectLatest(). A red flag is confusing conflate() with collectLatest().
What's really being asked
This question tests your understanding of the core execution model of Kotlin Flows. The interviewer wants to know if you grasp that by default, emit is a suspending call that waits for the collector, providing implicit backpressure. They are testing your ability to articulate when and why you would break this default behavior using specific operators, demonstrating a nuanced understanding of concurrency and data processing strategies.
The full answer
An excellent answer addresses four key points in order. First, explain Flow's default backpressure: emit suspends until the collector is ready, creating a sequential, lock-step process. Second, describe buffer(), which runs the producer and collector concurrently with a buffer (default size 64) in between. Use this to absorb bursts when you must process every single item. Third, explain conflate(), which drops intermediate emitted values if the collector is busy, ensuring only the most recent value is processed next. This is for when the latest state is all that matters. Fourth, detail collectLatest(), a terminal operator that cancels the collector's processing block for a given item if a new one is emitted, and restarts the block with the new item. This is for cancelling and restarting expensive, long-running work.
The mistakes people make
The most common error is confusing conflate() and collectLatest(). conflate() drops incoming data before it reaches the collector's block. collectLatest() accepts the data but cancels the action being performed on the previous item. Another red flag is not knowing that Flows handle backpressure by default and believing these operators are the only mechanism. A weak answer just says "they make things faster" without explaining the distinct semantic trade-offs of buffering, dropping, or cancelling.
What usually comes next
Expect follow-ups like: "When would you use buffer(Channel.UNLIMITED) and what are the risks?" (Answer: Almost never, due to high risk of OutOfMemoryError). Or, "How does flowOn() interact with these operators?" (Answer: flowOn changes the upstream execution context, while these operators manage concurrency between the already-established producer and collector contexts).
A concrete example
Consider a flow emitting user search input every 50ms, with a collector triggering a 300ms network request. Using collectLatest() is ideal. As the user types, any in-flight 300ms network request for a previous, now-outdated query is immediately cancelled, and a new one is started with the latest text. Using conflate() would be wrong here, as it wouldn't cancel the long-running network call, it would just drop the intermediate text inputs.
Interview question
When processing a rapid stream of UI events, where each event triggers an expensive, cancellable background task, which Flow operator ensures only the latest event's task runs, cancelling any prior in-progress tasks?
- a.conflate()
- b.Relying on Flow's default backpressure mechanism
- c.collectLatest()Correct
- d.buffer()
Why? this is the answer
collectLatest() is designed to cancel the processing block for a previous item if a new one is emitted, restarting the operation with the latest data. In contrast, conflate() only drops intermediate values, ensuring the collector receives the most recent value, but it does not cancel any long-running work already initiated for a prior item.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
- #kotlin
- #coroutines
- #flow
- #backpressure
- #android
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles