Challenge - 5 Problems
Coroutine Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested coroutine scopes
What is the output of this Kotlin code using coroutine scopes and structured concurrency?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { launch { delay(100) println("Task 1 done") } coroutineScope { launch { delay(50) println("Task 2 done") } println("Inside coroutineScope") } println("Outside coroutineScope") }
Attempts:
2 left
💡 Hint
Remember that coroutineScope waits for all its child coroutines to complete before continuing.
✗ Incorrect
The coroutineScope block prints "Inside coroutineScope" immediately, then waits for its child coroutine (Task 2) to finish. After that, the outer launch coroutine (Task 1) finishes, then "Outside coroutineScope" is printed.
❓ Predict Output
intermediate2:00remaining
Effect of cancelling a parent coroutine scope
What will be printed when this Kotlin code runs?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val parentJob = launch { launch { delay(100) println("Child 1 finished") } launch { delay(200) println("Child 2 finished") } } delay(150) parentJob.cancel() delay(200) println("Done") }
Attempts:
2 left
💡 Hint
Cancelling a parent coroutine cancels all its children immediately.
✗ Incorrect
Child 1 finishes before the parentJob is cancelled (after 100ms). Child 2 is cancelled before it can finish (200ms delay). Then "Done" is printed.
🔧 Debug
advanced2:00remaining
Identify the error in coroutine scope usage
This Kotlin code throws an exception. What is the cause?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val job = GlobalScope.launch { delay(100) println("Hello from GlobalScope") } job.join() println("Done") }
Attempts:
2 left
💡 Hint
GlobalScope is not tied to the lifecycle of runBlocking.
✗ Incorrect
Using GlobalScope.launch launches a coroutine not bound to the runBlocking scope, breaking structured concurrency. Although this code runs, it is considered a bad practice and can cause bugs in real apps.
📝 Syntax
advanced2:00remaining
Correct syntax for coroutineScope usage
Which option correctly uses coroutineScope to launch two child coroutines and waits for both to finish?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { // Fill in the blank }
Attempts:
2 left
💡 Hint
coroutineScope waits for all child coroutines inside it to complete before returning.
✗ Incorrect
Option A correctly uses coroutineScope to launch two child coroutines and waits for both to finish before continuing. Option A nests coroutineScope inside launch which delays waiting. Option A launches one coroutine outside coroutineScope, so it may not wait. Option A uses runBlocking inside runBlocking which is invalid.
🚀 Application
expert2:00remaining
How many coroutines are active at peak in this code?
Consider this Kotlin code snippet. How many coroutines run concurrently at the peak?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { launch { repeat(3) { i -> launch { delay(100L * i) println("Inner coroutine $i done") } } } launch { delay(50) println("Another coroutine done") } }
Attempts:
2 left
💡 Hint
Count all coroutines launched that overlap in time at the peak.
✗ Incorrect
The outer launch starts 3 inner coroutines immediately. Another separate launch starts concurrently. At peak, all 4 coroutines run concurrently before delays cause some to finish.