Bird
Raised Fist0
Kotlinprogramming~20 mins

Combining flows (zip, combine) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Flow Combiner Master
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 Kotlin flow zip example?
Consider the following Kotlin code using zip on two flows. What will be printed?
Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val flow1 = flowOf(1, 2, 3)
    val flow2 = flowOf("A", "B", "C", "D")

    flow1.zip(flow2) { a, b -> "$a$b" }
        .collect { println(it) }
}
A
1A
2B
3C
4D
B
1A
2B
3C
C
1A
2B
3C
D
D
A1
B2
C3
Attempts:
2 left
💡 Hint
Remember that zip pairs elements until one flow runs out.
❓ Predict Output
intermediate
2:00remaining
What does this Kotlin flow combine example print?
Look at this Kotlin code using combine on two flows. What will be the output?
Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.delay

fun main() = runBlocking {
    val flow1 = flow {
        emit(1)
        delay(100)
        emit(2)
    }
    val flow2 = flow {
        emit("A")
        delay(50)
        emit("B")
    }

    flow1.combine(flow2) { a, b -> "$a$b" }
        .collect { println(it) }
}
A
1A
2A
2B
B
1A
2A
1B
2B
C
1A
1B
2B
D
1A
2B
Attempts:
2 left
💡 Hint
combine emits every time any flow emits, using the latest values from both.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin flow zip code cause a compilation error?
This Kotlin code tries to zip two flows but fails to compile. What is the cause?
Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val flow1 = flowOf(1, 2, 3)
    val flow2 = flowOf("A", "B", "C")

    val zipped = flow1.zip(flow2) { a, b -> a + b }
    zipped.collect { println(it) }
}
ARuntime error: NullPointerException
BCompilation error: Missing collect call on zipped flow
CNo error, prints 1A 2B 3C
DCompilation error: Cannot add Int and String with '+' operator
Attempts:
2 left
💡 Hint
Check the types used in the lambda inside zip.
📝 Syntax
advanced
2:00remaining
Which option correctly combines two flows with combine operator?
Select the Kotlin code snippet that correctly uses combine to merge two flows of Int and String.
Aflow1.combine(flow2) { a, b -> "$a$b" }
Bflow1.combine(flow2) { a, b -> a + b }
Cflow1.zip(flow2) { a, b -> "$a$b" }
Dflow1.combine(flow2) { a, b -> a * b }
Attempts:
2 left
💡 Hint
combine requires a lambda that returns a value combining both inputs.
🚀 Application
expert
2:00remaining
How many items are emitted by this zipped flow?
Given these two Kotlin flows, how many items will the zipped flow emit?
Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val flow1 = flow {
        emit(10)
        emit(20)
        emit(30)
        emit(40)
    }
    val flow2 = flow {
        emit("X")
        emit("Y")
    }

    val zipped = flow1.zip(flow2) { a, b -> "$a$b" }
    var count = 0
    zipped.collect {
        count++
    }
    println(count)
}
A2
B4
C6
D0
Attempts:
2 left
💡 Hint
zip emits pairs until one flow runs out of elements.

Practice

(1/5)
1. What does the zip function do when combining two Kotlin flows?
easy
A. Pairs values one by one from both flows, emitting pairs only when both have emitted.
B. Merges the latest values from both flows whenever either emits.
C. Combines flows by concatenating their emitted values into a single list.
D. Filters values from the first flow based on the second flow's emissions.

Solution

  1. Step 1: Understand zip behavior

    The zip function pairs values from two flows one by one, waiting for both to emit before producing a pair.
  2. Step 2: Compare with other options

    combine merges latest values on any emission, not zip. Other options describe different behaviors.
  3. Final Answer:

    Pairs values one by one from both flows, emitting pairs only when both have emitted. -> Option A
  4. Quick Check:

    zip pairs values one by one = A [OK]
Hint: Remember: zip waits for both flows to emit one value each [OK]
Common Mistakes:
  • Confusing zip with combine behavior
  • Thinking zip merges latest values on any emission
  • Assuming zip concatenates values into a list
2. Which of the following is the correct syntax to combine two flows flowA and flowB using combine in Kotlin?
easy
A. flowA.combine(flowB) { a, b -> a + b }
B. flowA.zip(flowB) { a, b -> a + b }
C. combine(flowA flowB) { a, b -> a + b }
D. flowA.merge(flowB) { a, b -> a + b }

Solution

  1. Step 1: Recall combine extension syntax

    combine is called as an extension on one flow with another flow as argument and a lambda combining values.
  2. Step 2: Check options

    flowA.combine(flowB) { a, b -> a + b } matches the correct syntax. flowA.zip(flowB) { a, b -> a + b } uses zip, C uses top-level combine which is invalid, D uses non-existent merge with lambda.
  3. Final Answer:

    flowA.combine(flowB) { a, b -> a + b } -> Option A
  4. Quick Check:

    combine is an extension function = B [OK]
Hint: combine is called as flowA.combine(flowB) with lambda [OK]
Common Mistakes:
  • Using zip instead of combine
  • Trying to call combine as a top-level function
  • Using merge with a lambda (not valid)
3. What will be the output of the following Kotlin code?
val flow1 = flowOf(1, 2, 3)
val flow2 = flowOf("A", "B", "C", "D")

runBlocking {
  flow1.zip(flow2) { a, b -> "$a$b" }
    .collect { println(it) }
}
medium
A. 1A 2B 3C 4D
B. 1A 2B 3C
C. 1A 2B 3C D
D. A1 B2 C3 D4

Solution

  1. Step 1: Understand zip behavior with unequal flow lengths

    zip pairs elements one by one until the shortest flow ends. Here, flow1 has 3 elements, flow2 has 4.
  2. Step 2: Determine output pairs

    Pairs: (1, "A"), (2, "B"), (3, "C"). The extra "D" in flow2 is ignored.
  3. Final Answer:

    1A 2B 3C -> Option B
  4. Quick Check:

    zip stops at shortest flow length = C [OK]
Hint: zip stops when shortest flow ends, extra values ignored [OK]
Common Mistakes:
  • Expecting zip to emit all values from longer flow
  • Confusing zip with combine behavior
  • Mixing order of concatenation in output
4. Identify the error in this Kotlin code using combine and fix it:
val flow1 = flowOf(1, 2)
val flow2 = flowOf("X", "Y")

runBlocking {
  flow1.combine(flow2) { a, b -> "$a$b" }
    .collect { println(it) }
}
medium
A. No error; code runs and prints combined values.
B. Lambda parameters should be reversed: { b, a -> ... }.
C. Cannot call combine on flowOf; use zip instead.
D. Missing import for combine; add import kotlinx.coroutines.flow.combine.

Solution

  1. Step 1: Check for common mistakes with combine

    combine is an extension function requiring import from kotlinx.coroutines.flow.
  2. Step 2: Identify missing import

    Without import kotlinx.coroutines.flow.combine, code won't compile. Other options are incorrect because combine works on flowOf and lambda parameter order is correct.
  3. Final Answer:

    Missing import for combine; add import kotlinx.coroutines.flow.combine -> Option D
  4. Quick Check:

    Missing import causes compile error = D [OK]
Hint: Always import combine from kotlinx.coroutines.flow [OK]
Common Mistakes:
  • Forgetting to import combine function
  • Thinking combine is not available on flowOf
  • Swapping lambda parameters incorrectly
5. You want to combine two flows flowA and flowB so that every time either emits, you get the latest values from both. Which function should you use and why?
hard
A. Use merge because it merges emissions into a single flow without pairing.
B. Use zip because it pairs values one by one, ensuring synchronized pairs.
C. Use combine because it emits on any flow emission with latest values from both.
D. Use flatMapConcat to flatten emissions from both flows sequentially.

Solution

  1. Step 1: Understand the requirement

    You want to emit combined latest values whenever either flow emits, not just pair one by one.
  2. Step 2: Match function behavior

    combine merges latest values from both flows on any emission. zip waits for pairs, merge just merges emissions without combining, and flatMapConcat flattens sequences.
  3. Final Answer:

    Use combine because it emits on any flow emission with latest values from both. -> Option C
  4. Quick Check:

    combine merges latest values on any emission = A [OK]
Hint: combine emits latest from both on any emission, zip waits for pairs [OK]
Common Mistakes:
  • Choosing zip instead of combine for latest values
  • Confusing merge with combine
  • Using flatMapConcat which flattens sequences, not combines
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