0
0
Kotlinprogramming~20 mins

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

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