Challenge 2: A StateFlow Counter — Solution import kotlinx.coroutines.* import kotlinx.coroutines.flow.* fun main() = runBlocking { val counter = MutableStateFlow(0) val collectorJob = launch { counter.collect { value -> println("Counter is now: $value") } } delay(100) counter.value = 1 delay(100) counter.value = 2 delay(100) counter.value = 3 delay(100) collectorJob.cancel() } /* Output: Counter is now: 0 Counter is now: 1 Counter is now: 2 Counter is now: 3 Notes: - The collector immediately receives the initial value (0) as soon as it starts collecting, since StateFlow always holds a current value. - Each counter.value = ... assignment is picked up by the collecting coroutine and printed, since StateFlow is hot and broadcasts updates to every active collector. - collectorJob.cancel() stops the collecting coroutine at the end so runBlocking can finish — without cancelling it, the collect { } call would keep suspending forever waiting for more updates, and the program would never exit. */