Kotlin coroutines interview questions
Suspend functions, scopes, dispatchers, structured concurrency, cancellation and errors: the questions every senior Android loop opens with.
– interview-ready · 18 questions in this topic
– of 18 tried
Saved on this device. Interview-ready means you got it right on two different days. Guesses and hints count for less.
Questions
- What happens to viewModelScope work on rotation?Predict
- Coroutines vs threadsPredict
- What does suspend mean?Pick the fix
- launch vs asyncPredict · timeline
- Structured concurrencyPredict
- CoroutineScope vs CoroutineContextPredict
- Choosing a dispatcherPick the fix
- withContext and main-safetyFill the blank
- Cooperative cancellationSpot the bug
- Don't swallow CancellationExceptionSpot the bug
- Job vs SupervisorJobPredict
- Where CoroutineExceptionHandler worksPick the fix
- Loading several things in parallelOrder the steps
- withTimeout vs withTimeoutOrNullPredict
- Shared mutable statePick the fix
- Collecting safely from the UIPick the fix
- Why not GlobalScope?Pick the fix
- runBlocking on AndroidPredict
Practise this topic
Flashcards · 24
Flip, then say if you knew it. Needs JavaScript; every answer is also on its question page.
Quick quiz · 18 questions
One exercise per question, checked as you go. Needs JavaScript.
Mock interview · 3 sets
Timed, out loud, then tick what you covered. Needs JavaScript.
Fix this PR · 2 reviews
Review a diff like a senior: flag the lines you'd comment on. Needs JavaScript.
Quick facts
Recall the answer first, then tap Show to check.
Dispatchers.IO parallelism
Dispatchers.IO allows up to 64 threads (or the number of cores, if larger) and shares them with Dispatchers.Default.
viewModelScope context
viewModelScope is SupervisorJob() + Dispatchers.Main.immediate, and it is cancelled when the ViewModel is cleared, just before onCleared() runs.
CoroutineStart.LAZY
async(start = CoroutineStart.LAZY) doesn't run until you call start() or await().
yield()
yield() checks for cancellation and lets other coroutines on the same dispatcher run.
NonCancellable
withContext(NonCancellable) lets cleanup in a finally block suspend after the coroutine was cancelled.
runInterruptible
runInterruptible runs blocking code and interrupts its thread if the coroutine is cancelled.
Printable cheat sheet · Review your weak spots in this topic
Share your progress
Sources: Question outline adapted from Android-Interview-Questions by Mohsen Mirhoseini, licensed under Apache 2.0. We reorganised the questions, rewrote every answer and added new ones. Credits and license