Structured Concurrency & Cancellation
KOTLIN › Coroutines
Scoping coroutine lifetimes, isolating failure, and making cancellation actually take effect.
Structured concurrency is the backbone of correct coroutine code on Android, so interviewers probe whether you understand how scopes bound lifetimes, how failures and cancellation propagate, and when each is isolated. Expect to reason about coroutineScope vs supervisorScope, why cancellation is cooperative, and exactly when a CoroutineExceptionHandler does or does not fire. Strong answers connect these rules to real ViewModel/repository patterns rather than reciting API names.
What this covers
- Structured concurrency guarantees: scopes bound child lifetimes, parents wait for children, cancellation propagates
- coroutineScope vs supervisorScope and Job vs SupervisorJob for failure isolation between siblings
- supervisorScope isolates direct children only, not grandchildren
- Cooperative cancellation with isActive, ensureActive(), and yield(), and why CancellationException must be rethrown
- Blocking calls have no suspension point to cancel at: use runInterruptible, or cancel the resource yourself
- cancel() returns immediately; cancelAndJoin() waits for the coroutine to finish unwinding first
- Suspending cleanup in finally needs withContext(NonCancellable), used narrowly
- withTimeout throws a CancellationException subtype; withTimeoutOrNull returns null instead
- Parallel decomposition with async/await, started before either is awaited
- Inject dispatchers as constructor parameters; let ViewModels own the scope; never create private scopes deep in the stack
Study this topic
- Structured Concurrency & Cancellation explained: the guided lesson
- 22 practice quiz questions
- Structured Concurrency & Cancellation interview questions and answers