Bird
Raised Fist0
Kotlinprogramming~20 mins

Why Flow matters for async sequences in Kotlin - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Flow Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Flow code?
Consider this Kotlin Flow code that emits numbers with delay. What will be printed?
Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    val flow = flow {
        for (i in 1..3) {
            delay(100)
            emit(i)
        }
    }
    flow.collect { value ->
        println("Received $value")
    }
}
AReceived 1\nReceived 2\nReceived 3
BReceived 3\nReceived 2\nReceived 1
CReceived 1\nReceived 1\nReceived 1
DNo output, program hangs
Attempts:
2 left
💡 Hint
Flow emits values sequentially with delay, collect prints each as it arrives.
🧠 Conceptual
intermediate
1:30remaining
Why use Flow instead of a simple suspend function returning a list?
Which reason best explains why Kotlin Flow is preferred for asynchronous sequences over a suspend function that returns a full list?
AFlow always runs on the main thread, making UI updates easier.
BSuspend functions cannot return lists in Kotlin.
CFlow emits values one by one asynchronously, allowing processing before all data is ready.
DSuspend functions are slower than Flow in all cases.
Attempts:
2 left
💡 Hint
Think about how data arrives and is processed over time.
🔧 Debug
advanced
2:00remaining
What error does this Flow code produce?
This Kotlin code tries to collect from a Flow but crashes. What error is thrown?
Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    val flow = flow {
        emit(1)
        throw RuntimeException("Oops")
    }
    flow.collect { value ->
        println(value)
    }
}
ANo error, prints 1 then stops
BNullPointerException
CCompilation error: missing catch block
DRuntimeException with message 'Oops'
Attempts:
2 left
💡 Hint
Look at what happens after emitting the first value.
📝 Syntax
advanced
1:30remaining
Which option correctly creates a Flow emitting squares of 1 to 3?
Choose the Kotlin code snippet that correctly creates a Flow emitting 1, 4, 9.
Aflow { (1..3).map { emit(it * it) } }
Bflow { for (i in 1..3) emit(i * i) }
Cflow { for i in 1..3 emit(i * i) }
Dflow { emit(i * i) for i in 1..3 }
Attempts:
2 left
💡 Hint
Remember Kotlin syntax for loops and emit inside flow builder.
🚀 Application
expert
2:30remaining
How many items does this Flow emit before cancellation?
Given this Kotlin Flow code with cancellation, how many values are printed before the program stops?
Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    val flow = flow {
        var i = 1
        while (true) {
            emit(i++)
            delay(50)
        }
    }
    val job = launch {
        flow.collect { value ->
            println(value)
            if (value == 3) cancel()
        }
    }
    job.join()
}
A3
B4
CInfinite, program never stops
D2
Attempts:
2 left
💡 Hint
Look when cancellation happens inside collect.

Practice

(1/5)
1. What is the main advantage of using Flow in Kotlin for asynchronous sequences?
easy
A. It allows handling data streams asynchronously and efficiently over time.
B. It makes synchronous code run faster.
C. It replaces all uses of coroutines.
D. It automatically caches all data permanently.

Solution

  1. Step 1: Understand what Flow does

    Flow is designed to handle streams of data that come over time, allowing asynchronous processing.
  2. Step 2: Compare options

    It allows handling data streams asynchronously and efficiently over time. correctly describes Flow's main advantage. Options A, C, and D are incorrect or misleading.
  3. Final Answer:

    It allows handling data streams asynchronously and efficiently over time. -> Option A
  4. Quick Check:

    Flow = async data streams [OK]
Hint: Flow handles data over time asynchronously [OK]
Common Mistakes:
  • Thinking Flow speeds up synchronous code
  • Confusing Flow with coroutine replacement
  • Assuming Flow caches data permanently
2. Which of the following is the correct way to create a simple Flow emitting numbers 1 to 3 in Kotlin?
easy
A. val flow = flowOf(1, 2, 3)
B. val flow = listOf(1, 2, 3).asFlow()
C. val flow = flow { emit(1); emit(2); emit(3) }
D. All of the above

Solution

  1. Step 1: Review Flow creation methods

    Kotlin Flow can be created using flowOf(), asFlow() on collections, or flow builder with emit calls.
  2. Step 2: Check each option

    Options B, C, and D are all valid ways to create a Flow emitting 1, 2, 3.
  3. Final Answer:

    All of the above -> Option D
  4. Quick Check:

    Multiple ways to create Flow = A [OK]
Hint: Remember flowOf, asFlow, and flow builder all create Flows [OK]
Common Mistakes:
  • Thinking only flowOf creates Flow
  • Confusing List with Flow directly
  • Missing that flow builder emits values
3. What will be the output of this Kotlin code using Flow?
import kotlinx.coroutines.flow.*
suspend fun main() {
  val flow = flow {
    emit(10)
    emit(20)
    emit(30)
  }
  flow.collect { value -> println(value) }
}
medium
A. Compilation error due to missing coroutine scope
B. Only 30 printed
C. 10 20 30 printed each on a new line
D. No output because flow is cold

Solution

  1. Step 1: Understand flow emission and collection

    The flow emits 10, 20, and 30 sequentially, and collect prints each value.
  2. Step 2: Analyze output

    Each emitted value is printed on its own line, so output is 10, 20, 30 each on a new line.
  3. Final Answer:

    10 20 30 printed each on a new line -> Option C
  4. Quick Check:

    Flow emits and collect prints all values [OK]
Hint: Flow emits all values when collected [OK]
Common Mistakes:
  • Thinking only last value prints
  • Confusing cold flow behavior with no output
  • Assuming compile error without coroutine scope
4. Identify the error in this Kotlin Flow code snippet:
val flow = flow {
  emit(1)
  emit(2)
  emit(3)
}
flow.collect { println(it) }
medium
A. emit cannot be called inside flow builder
B. Missing suspend modifier on main or coroutine scope to call collect
C. collect should be replaced with collectLatest
D. No error, code runs fine as is

Solution

  1. Step 1: Check coroutine context for collect

    collect is a suspend function and must be called from a suspend function or coroutine scope.
  2. Step 2: Identify missing suspend or coroutine scope

    The snippet calls collect outside suspend context, causing a compile error.
  3. Final Answer:

    Missing suspend modifier on main or coroutine scope to call collect -> Option B
  4. Quick Check:

    collect needs suspend context [OK]
Hint: collect requires suspend or coroutine scope [OK]
Common Mistakes:
  • Thinking emit is invalid inside flow
  • Confusing collect with collectLatest necessity
  • Assuming code runs without coroutine context
5. You want to process a stream of user input events asynchronously and update the UI only with the latest event to avoid lag. Which Flow operator should you use in Kotlin?
hard
A. collectLatest()
B. collect()
C. map()
D. buffer()

Solution

  1. Step 1: Understand the problem context

    We want to handle user input events asynchronously and update UI with only the latest event to prevent lag.
  2. Step 2: Identify the correct Flow operator

    collectLatest cancels previous processing when a new event arrives, ensuring UI updates only with latest event.
  3. Final Answer:

    collectLatest() -> Option A
  4. Quick Check:

    Latest event processing = collectLatest [OK]
Hint: Use collectLatest to handle only newest events [OK]
Common Mistakes:
  • Using collect which processes all events
  • Using buffer which queues events but doesn't cancel
  • Confusing map with event handling operator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard