0
0
Kotlinprogramming~20 mins

SharedFlow for event broadcasting in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SharedFlow Mastery
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 SharedFlow example?

Consider this Kotlin code using MutableSharedFlow to broadcast events. What will be printed?

Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    val sharedFlow = MutableSharedFlow<Int>()

    launch {
        sharedFlow.collect { value ->
            println("Collector 1 received: $value")
        }
    }

    launch {
        delay(100)
        sharedFlow.emit(1)
        sharedFlow.emit(2)
    }

    launch {
        delay(200)
        sharedFlow.collect { value ->
            println("Collector 2 received: $value")
        }
    }

    delay(500)
}
A
Collector 1 received: 1
Collector 1 received: 2
B
Collector 1 received: 1
Collector 1 received: 2
Collector 2 received: 1
Collector 2 received: 2
C
Collector 1 received: 1
Collector 1 received: 2
Collector 2 received: 2
D
Collector 2 received: 1
Collector 2 received: 2
Attempts:
2 left
💡 Hint

Think about when Collector 2 starts collecting and what events it can receive.

🧠 Conceptual
intermediate
1:30remaining
What happens if replay is set to 1 in MutableSharedFlow?

In MutableSharedFlow(replay = 1), what does the replay parameter do?

AIt limits the number of collectors to 1 at a time.
BIt caches the last emitted value and replays it to new collectors when they start collecting.
CIt buffers only one event and drops the rest if collectors are slow.
DIt delays emission of events by 1 millisecond.
Attempts:
2 left
💡 Hint

Think about what replay means in the context of event streams.

🔧 Debug
advanced
2:30remaining
Why does this SharedFlow code cause a deadlock?

Examine this Kotlin code snippet. Why does it cause the program to hang indefinitely?

Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    val sharedFlow = MutableSharedFlow<Int>(replay = 0, extraBufferCapacity = 0)

    launch {
        sharedFlow.emit(1)
        println("Emitted 1")
    }

    delay(100)

    sharedFlow.collect { value ->
        println("Collected: $value")
    }
}
ABecause extraBufferCapacity is zero, causing a buffer overflow error.
BBecause replay is zero, so no values are buffered and emit fails silently.
CBecause emit suspends until there is a collector ready, but no collector is active when emit is called.
DBecause collect is called after emit, so it misses all events and terminates immediately.
Attempts:
2 left
💡 Hint

Think about how emit behaves when there are no active collectors and no buffer.

📝 Syntax
advanced
1:30remaining
Which option correctly creates a MutableSharedFlow with replay 2 and buffer capacity 3?

Choose the correct Kotlin code to create a MutableSharedFlow with replay = 2 and extraBufferCapacity = 3.

Aval flow = MutableSharedFlow<Int>(replayCount = 2, bufferCapacity = 3)
Bval flow = MutableSharedFlow<Int>(bufferCapacity = 3, replayCount = 2)
Cval flow = MutableSharedFlow<Int>(replay = 3, extraBufferCapacity = 2)
Dval flow = MutableSharedFlow<Int>(replay = 2, extraBufferCapacity = 3)
Attempts:
2 left
💡 Hint

Check the official parameter names for MutableSharedFlow constructor.

🚀 Application
expert
3:00remaining
How to ensure all collectors receive all events in SharedFlow?

You want to broadcast events so that every collector receives all emitted events, even if they start collecting late. Which SharedFlow configuration achieves this?

AUse <code>MutableSharedFlow(replay = Int.MAX_VALUE)</code> to buffer all events indefinitely.
BUse <code>MutableSharedFlow(replay = 0, extraBufferCapacity = 0)</code> and start collectors before emitting.
CUse <code>MutableSharedFlow(replay = 1)</code> and emit events slowly to avoid loss.
DUse <code>MutableSharedFlow(replay = 0, extraBufferCapacity = Int.MAX_VALUE)</code> to buffer all events.
Attempts:
2 left
💡 Hint

Think about how replay buffers past events for new collectors.