Skip to content
tezvyn:

Coroutines

46 bites tagged Coroutines — interview questions with model answers, and 60-second explainers.

Android & Kotlin2 min read

Difference between launch and async in Kotlin Coroutines

This tests your grasp of coroutine result handling and exception propagation. Explain that `launch` is fire-and-forget (returns `Job`), while `async` is for results (returns `Deferred`). Mention `async` defers exceptions until `await()`.

Android & Kotlin2 min read

Coroutine Exception Handling: launch vs. async

Coroutine builders handle exceptions differently. `launch` propagates them immediately, crashing if unhandled. `async` silently catches them, waiting for you to call `await`. Use a `CoroutineExceptionHandler` on root scopes for global logging.

Android & Kotlin2 min read

Which side-effect handler for a one-time coroutine action in Compose?

This tests your understanding of Compose lifecycles and side-effect handlers. A good answer names `LaunchedEffect(Unit)`, explaining the constant key ensures the effect runs only once. A red flag is suggesting launching coroutines on every recomposition.

Android & Kotlin2 min read

How does coroutine cancellation work internally?

This tests your grasp of cooperative cancellation. Explain that `cancel()` sets a flag, causing a `CancellationException` at the next suspension point. A non-suspending loop must explicitly check `isActive` to stop.

Android & Kotlin2 min read

Explain backpressure in Kotlin Flows and its management operators

This tests your understanding of Flow's sequential nature and strategies for decoupling producers and consumers. Explain default suspension, then detail buffer(), conflate(), and collectLatest(). A red flag is confusing conflate() with collectLatest().

Android & Kotlin2 min read

What is a CoroutineDispatcher and when to use IO vs Default?

Tests your grasp of thread pool strategy for CPU vs. I/O-bound tasks. A good answer defines Dispatchers, contrasts the fixed-size Default pool (for CPU work) with the larger IO pool (for blocking I/O), and gives clear examples.

Android & Kotlin2 min read

Hot vs. Cold Streams: StateFlow vs. Flow in Android

This tests your grasp of stream lifecycles. A great answer defines cold `Flow` (lazy, per-collector) vs. hot `StateFlow` (active, shared state) and uses a ViewModel UI state scenario. A red flag is mischaracterizing a `Flow` as a one-time operation.

Android & Kotlin2 min read

Explain Structured Concurrency in Kotlin Coroutines

Tests your grasp of coroutine lifecycle management. A great answer defines the parent-child relationship within a CoroutineScope, explains cancellation propagation, and details how exceptions cancel the entire hierarchy.

Android & Kotlin2 min read

What is a Kotlin `suspend` function and how does it work?

Tests your grasp of Kotlin's non-blocking concurrency. A great answer defines a suspend function as pausable, states its two calling rules (from another suspend function or coroutine builder), and mentions the compiler's CPS transformation.

Android & Kotlin2 min read

Explain launch vs. async in Kotlin Coroutines

Tests your grasp of structured concurrency, return values, and exceptions. `launch` is for fire-and-forget tasks (returns `Job`), while `async` is for tasks that produce a result (returns `Deferred`).

Android & Kotlin2 min read

Kotlin's Job: A Handle to a Background Task

A Job is a handle to a coroutine, letting you manage its lifecycle. You get one from `launch` to control background tasks like network calls. The key footgun: a child's *failure* (Exception) cancels its parent, but a child's *cancellation* does not.

Python & FastAPI2 min read

Python Coroutines: Functions You Can Pause and Resume

A Python coroutine is a function that can be paused and resumed. It yields control during I/O waits, allowing other tasks to run instead of blocking the program. The main footgun: calling an `async` function does nothing; you must `await` it to run it.

Android & Kotlin2 min read

CoroutineWorker: Background Tasks with Coroutines

CoroutineWorker simplifies background tasks by letting you use suspend functions inside a WorkManager job. It's for deferrable work, like syncing data, that needs to survive process death.

Android & Kotlin2 min read

Testing Coroutines: Control Time with `runTest`

Test asynchronous coroutine code as if it were synchronous. The `kotlinx-coroutines-test` library lets you control a virtual clock. Use `runTest` to wrap tests calling suspend functions, ensuring your test waits for async work before asserting results.

Android & Kotlin2 min read

Handling API State with a Sealed Class Wrapper

Treat API calls as states, not just data. A sealed class wrapper (`Success`, `Error`, `Loading`) models these states for your UI. Use this with Retrofit to show spinners or errors without crashing. The footgun: don't scatter try-catch blocks; centralize them.

Android & Kotlin2 min read

SharedFlow: A Hot Flow for Broadcasting Events

A SharedFlow is a hot stream that broadcasts values to all subscribers, like a live TV channel. It's ideal for one-to-many events, like UI updates or notifications. The main footgun: it never completes, so `collect` will suspend forever.

Android & Kotlin2 min read

StateFlow: A Hot Flow for UI State

StateFlow is a state-holder observable that emits the current and new state updates to its collectors. Use it to expose UI state from a ViewModel to a UI. The footgun: slow collectors can miss intermediate state updates, receiving only the most recent value.

Android & Kotlin2 min read

Kotlin Coroutines: async/await for Parallel Results

async starts a coroutine for a parallel result, returning a `Deferred` value. `await` pauses until that result is ready. Use it to run independent network calls concurrently. The footgun: using it for sequential tasks creates a needless race condition.

Android & Kotlin2 min read

Structured Concurrency in Kotlin

Structured concurrency treats async operations like code blocks: a parent task's lifetime contains its children. In Kotlin, a `CoroutineScope` ensures if the parent is cancelled, all its child coroutines are too.

Android & Kotlin2 min read

Kotlin Flow: Asynchronous Data Streams

A Kotlin Flow is like an async Sequence, emitting multiple values over time without blocking. It's used for live data from a database or streaming network responses. The footgun: Flows are cold; the code doesn't run until a collector calls `.collect()`.

Android & Kotlin2 min read

CoroutineScope: The Parent of Your Coroutines

A CoroutineScope acts as a parent to a group of coroutines, managing their lifecycles. When the parent scope is cancelled, all its children are cancelled too. It's used with builders like `launch` to group related work, like in an Android ViewModel.

Android & Kotlin2 min read

Coroutine Dispatchers: Telling Your Coroutines Which Thread to Use

A Dispatcher tells a coroutine which thread or thread pool to use for its work. Use `Dispatchers.Default` for CPU-intensive tasks. If none is specified, it inherits from its parent. The main footgun: `Unconfined` can resume on an unexpected thread.

Get Coroutines bites daily.

Five a day, five minutes, offline. With quizzes so it sticks.

Open testing — you’ll join as an early tester.