0
0
Kotlinprogramming~20 mins

Flow operators (map, filter, transform) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flow Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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) }
}
A6
B
2
4
6
8
10
C
3
6
9
12
15
D
4
8
12
16
20
Attempts:
2 left
💡 Hint
Remember map transforms each item, then filter keeps only those divisible by 3.
Predict Output
intermediate
2: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) }
}
A
1
2
3
B
1
2
3
10
20
30
C
10
20
30
D
1
10
2
20
3
30
Attempts:
2 left
💡 Hint
transform can emit multiple values per input.
🧠 Conceptual
advanced
1:30remaining
Behavior of filter with suspend predicate
Which statement about the filter operator in Kotlin Flow is true when using a suspend predicate?
Afilter cannot use suspend functions as predicates.
Bfilter evaluates the predicate synchronously and blocks the flow.
Cfilter supports suspend predicates and evaluates them asynchronously for each element.
Dfilter only works with non-suspend predicates and throws a compile error otherwise.
Attempts:
2 left
💡 Hint
Check the official Kotlin Flow documentation about filter operator.
Predict Output
advanced
2: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) }
}
A
2
3
4
5
B
1
2
2
3
C
2
4
3
5
D
3
4
5
6
Attempts:
2 left
💡 Hint
map doubles each number with delay, transform emits the number and number+1.
🔧 Debug
expert
2: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) }
}
Afilter operator causes the exception because it cannot handle negative numbers.
BThe exception is thrown inside map and stops the flow collection immediately.
CThe flow completes normally without exceptions because filter ignores errors.
DThe exception is caught and suppressed by the flow operators automatically.
Attempts:
2 left
💡 Hint
Check where the exception is thrown and how flows handle exceptions.