Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which Kotlin Flow operator is used to change each item in the flow to a new value?
easy
A. filter
B. map
C. transform
D. collect

Solution

  1. Step 1: Understand the purpose of map

    The map operator changes each item in the flow to a new value by applying a function.
  2. Step 2: Compare with other operators

    filter keeps items based on a condition, transform can emit multiple items, and collect gathers items but does not change them.
  3. Final Answer:

    map -> Option B
  4. Quick Check:

    Change each item = map [OK]
Hint: Map means change each item to a new one [OK]
Common Mistakes:
  • Confusing map with filter
  • Thinking transform only changes one item
  • Using collect to change items
2. Which of the following is the correct syntax to filter even numbers from a Kotlin Flow named numbers?
easy
A. numbers.collect { it % 2 == 0 }
B. numbers.map { it % 2 == 0 }
C. numbers.filter { it % 2 == 0 }
D. numbers.transform { emit(it % 2 == 0) }

Solution

  1. Step 1: Identify the filter syntax

    The filter operator takes a lambda returning a Boolean to keep items matching the condition.
  2. Step 2: Check each option

    numbers.filter { it % 2 == 0 } correctly uses filter { it % 2 == 0 } to keep even numbers. numbers.map { it % 2 == 0 } uses map which changes items to booleans. numbers.transform { emit(it % 2 == 0) } emits booleans instead of filtering. numbers.collect { it % 2 == 0 } misuses collect.
  3. Final Answer:

    numbers.filter { it % 2 == 0 } -> Option C
  4. Quick Check:

    Filter even numbers = filter { it % 2 == 0 } [OK]
Hint: Filter keeps items; map changes items [OK]
Common Mistakes:
  • Using map instead of filter
  • Misusing collect as a filter
  • Confusing transform syntax
3. What is the output of this Kotlin Flow code?
flowOf(1, 2, 3, 4)
    .filter { it % 2 == 0 }
    .map { it * 10 }
    .toList()
medium
A. [20, 40]
B. [10, 20]
C. [2, 4]
D. [1, 2, 3, 4]

Solution

  1. Step 1: Apply filter to keep even numbers

    The flow starts with [1, 2, 3, 4]. After filtering even numbers, we get [2, 4].
  2. Step 2: Apply map to multiply each by 10

    Mapping [2, 4] by multiplying each by 10 results in [20, 40].
  3. Final Answer:

    [20, 40] -> Option A
  4. Quick Check:

    Filter evens then multiply by 10 = [20, 40] [OK]
Hint: Filter first, then map to change values [OK]
Common Mistakes:
  • Mapping before filtering
  • Confusing output with original list
  • Ignoring order of operators
4. Identify the error in this Kotlin Flow code snippet:
flowOf(1, 2, 3)
    .filter { it > 1 }
    .map { it * 2 }
    .transform { emit(it + 1) }
medium
A. Using transform after map is invalid
B. The filter condition syntax is wrong
C. The emit call inside transform is incorrect
D. The code misses a terminal operator like collect

Solution

  1. Step 1: Check the flow operators usage

    The operators filter, map, and transform are used correctly in sequence.
  2. Step 2: Identify missing terminal operation

    Flows are cold and need a terminal operator like collect or toList to start processing. This code lacks that.
  3. Final Answer:

    The code misses a terminal operator like collect -> Option D
  4. Quick Check:

    Flows need terminal operator to run [OK]
Hint: Flows need collect or toList to run [OK]
Common Mistakes:
  • Forgetting terminal operator
  • Thinking transform can't follow map
  • Misreading filter condition syntax
5. Given a Flow of integers, how can you use transform to emit both the original number and its square for each item?
hard
A. flow.transform { emit(it); emit(it * it) }
B. flow.map { listOf(it, it * it) }
C. flow.filter { emit(it); emit(it * it) }
D. flow.collect { emit(it); emit(it * it) }

Solution

  1. Step 1: Understand transform usage

    transform allows emitting zero or more items per input, so emitting both original and square is possible.
  2. Step 2: Check each option's correctness

    flow.transform { emit(it); emit(it * it) } correctly uses transform with two emit calls. flow.map { listOf(it, it * it) } uses map which emits a list, not separate items. flow.filter { emit(it); emit(it * it) } misuses filter. flow.collect { emit(it); emit(it * it) } misuses collect which is terminal and cannot emit.
  3. Final Answer:

    flow.transform { emit(it); emit(it * it) } -> Option A
  4. Quick Check:

    Emit multiple items per input = transform [OK]
Hint: Use transform to emit multiple items per input [OK]
Common Mistakes:
  • Using map to emit multiple items separately
  • Misusing filter or collect for emission
  • Not calling emit inside transform
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