Android Developer interview question
What is structured concurrency in Kotlin coroutines?
Structured concurrency binds child coroutines to a parent scope, so completion, cancellation, and failure have a defined owner; use lifecycle-aware scopes and avoid unowned GlobalScope work.
Direct answer
What a strong answer covers
How it works
Every coroutine is launched inside a CoroutineScope, and the scope’s Job becomes the parent of the new coroutine. Cancelling the parent cancels the children, an uncaught failure in a child cancels its siblings and the parent, and a parent does not complete until every child has. Cancellation is cooperative: suspending functions check it at suspension points, and CPU loops must call ensureActive or yield. This turns lifecycles into a tree the compiler and the runtime can enforce, which is why GlobalScope, with no parent, is treated as a leak by default.
Example
A screen that starts three network calls in viewModelScope sees all three cancelled when the user leaves, and a failure in one cancels the other two unless they run under supervisorScope. A file export that must outlive the screen is handed to an application-scoped CoroutineScope owned by a repository rather than launched from the ViewModel.
Common mistakes
- Launching in GlobalScope from a ViewModel and leaking work past the screen.
- Catching CancellationException in a generic catch block and preventing cancellation from propagating.
- Running a tight loop without suspension points so cancellation never takes effect.
A useful answer structure
Answer from your own work.
Do not memorise a script: use only examples you can explain honestly in follow-up questions.
Open the desktop workspace and rehearse a clear answer in your own words.