Interview frequency: …
Short answer
It is an organizing principle that ensures no coroutine leaks, by arranging coroutines in a hierarchy as children of a parent that does not finish until all its children finish, and if the parent is cancelled all its children are cancelled automatically.
Explanation
Structured concurrency prevents the problem of dangling tasks that run in the background and waste memory and battery after the user leaves the screen. When the ViewModel's lifecycle ends (onCleared) or the Activity's does, the CoroutineScope cancels the parent job, which propagates the cancellation signal immediately to all the operations and children branching from it.
Code example
// coroutineScope Ensures the function does not return until all child tasks complete:
suspend fun processOrder() = coroutineScope {
launch { validateInventory() }
launch { reservePayment() }
// processOrder completes only if both succeed; if one fails, the other is cancelled immediately
}Common mistakes
- Using GlobalScope.launch, which breaks structured concurrency and isolates the coroutine from any lifecycle, causing a serious memory leak.