Challenge - 5 Problems
Structured Concurrency Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of coroutine scope with structured concurrency
What is the output of this Kotlin code using structured concurrency?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { launch { delay(100) println("Task 1 done") } launch { delay(50) println("Task 2 done") } println("Main done") }
Attempts:
2 left
💡 Hint
Remember that runBlocking waits for all launched coroutines inside it to complete before finishing.
✗ Incorrect
runBlocking waits for all child coroutines launched inside it to finish before it completes. So "Main done" prints first, then the two tasks print after their delays.
🧠 Conceptual
intermediate1:30remaining
Why does structured concurrency prevent coroutine leaks?
Which statement best explains why structured concurrency prevents coroutine leaks in Kotlin?
Attempts:
2 left
💡 Hint
Think about how coroutine lifecycles are managed in structured concurrency.
✗ Incorrect
Structured concurrency ties child coroutines to a parent scope, so the parent waits for children to finish or cancels them, preventing orphaned coroutines that leak resources.
🔧 Debug
advanced2:00remaining
Identify the leak caused by missing coroutine scope
What error or problem occurs when launching a coroutine without a proper parent scope in Kotlin?
Kotlin
import kotlinx.coroutines.* fun main() { GlobalScope.launch { delay(1000) println("Leaked coroutine finished") } println("Main finished") }
Attempts:
2 left
💡 Hint
Consider what happens when coroutines are launched in GlobalScope.
✗ Incorrect
Using GlobalScope launches coroutines that are not tied to any lifecycle, so they can outlive the caller and cause leaks.
📝 Syntax
advanced2:00remaining
Correct syntax for structured concurrency with coroutineScope
Which option correctly uses structured concurrency to launch two child coroutines and waits for both to finish?
Kotlin
import kotlinx.coroutines.* suspend fun doWork() = coroutineScope { // launch two child coroutines here }
Attempts:
2 left
💡 Hint
Inside coroutineScope, use launch to start child coroutines tied to the scope.
✗ Incorrect
Using launch inside coroutineScope creates child coroutines that the scope waits for before completing, ensuring structured concurrency.
🚀 Application
expert2:30remaining
How structured concurrency handles exceptions to prevent leaks
Given this code, what happens when one child coroutine throws an exception inside coroutineScope?
Kotlin
import kotlinx.coroutines.* suspend fun main() = coroutineScope { launch { delay(100) throw RuntimeException("Error in child 1") } launch { delay(200) println("Child 2 finished") } }
Attempts:
2 left
💡 Hint
Think about how coroutineScope handles exceptions from child coroutines.
✗ Incorrect
coroutineScope cancels all children if any child throws an exception, preventing leaks by not leaving orphan coroutines running.