Challenge - 5 Problems
Flow Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of map and filter on a Flow
What is the output of this Kotlin Flow code snippet?
Kotlin
import kotlinx.coroutines.flow.* import kotlinx.coroutines.runBlocking fun main() = runBlocking { val flow = (1..5).asFlow() .map { it * 2 } .filter { it % 3 == 0 } flow.collect { println(it) } }
Attempts:
2 left
💡 Hint
Remember map transforms each item, then filter keeps only those divisible by 3.
✗ Incorrect
The flow emits numbers 1 to 5. map multiplies each by 2: 2,4,6,8,10. filter keeps only those divisible by 3, which is only 6.
❓ Predict Output
intermediate2:00remaining
Transform operator output
What does this Kotlin Flow code print?
Kotlin
import kotlinx.coroutines.flow.* import kotlinx.coroutines.runBlocking fun main() = runBlocking { val flow = (1..3).asFlow() .transform { emit(it) emit(it * 10) } flow.collect { println(it) } }
Attempts:
2 left
💡 Hint
transform can emit multiple values per input.
✗ Incorrect
For each number 1 to 3, transform emits the number itself and then the number multiplied by 10.
🧠 Conceptual
advanced1:30remaining
Behavior of filter with suspend predicate
Which statement about the filter operator in Kotlin Flow is true when using a suspend predicate?
Attempts:
2 left
💡 Hint
Check the official Kotlin Flow documentation about filter operator.
✗ Incorrect
The filter operator in Kotlin Flow supports suspend predicates, allowing asynchronous evaluation for each element.
❓ Predict Output
advanced2:00remaining
Output of combined map and transform with delay
What is the output of this Kotlin Flow code?
Kotlin
import kotlinx.coroutines.flow.* import kotlinx.coroutines.runBlocking import kotlinx.coroutines.delay fun main() = runBlocking { val flow = (1..2).asFlow() .map { delay(100) it * 2 } .transform { emit(it) emit(it + 1) } flow.collect { println(it) } }
Attempts:
2 left
💡 Hint
map doubles each number with delay, transform emits the number and number+1.
✗ Incorrect
Numbers 1 and 2 are doubled to 2 and 4 with delay. transform emits each doubled number and that number plus one.
🔧 Debug
expert2:30remaining
Why does this Flow code throw an exception?
This Kotlin Flow code throws an exception at runtime. What is the cause?
Kotlin
import kotlinx.coroutines.flow.* import kotlinx.coroutines.runBlocking fun main() = runBlocking { val flow = (1..3).asFlow() .map { if (it == 2) throw IllegalStateException("Error at 2") else it } .filter { it > 0 } flow.collect { println(it) } }
Attempts:
2 left
💡 Hint
Check where the exception is thrown and how flows handle exceptions.
✗ Incorrect
The exception is thrown inside the map operator when the value is 2, which stops the flow collection immediately.