Skip to content
tezvyn:

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

Source: kotlinlang.orgEasyHow cards are made

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.

What's really being asked

This question tests your fundamental understanding of the mechanism that makes Kotlin coroutines efficient and lightweight. The interviewer wants to know if you can articulate how a function can pause without blocking a thread. It's a foundational concept that separates a surface-level understanding from a deep one, and it's a gateway to discussing dispatchers, structured concurrency, and error handling.

The full answer

A strong answer covers three key areas in order. First, the definition: a function marked with the suspend keyword, which signals to the compiler it can be paused and resumed later. This allows long-running operations to yield the thread. Second, the two main rules of invocation: a suspend function can only be called from another suspend function or from a coroutine builder like launch or async, which creates the initial coroutine scope. Third, the high-level compiler implementation: the compiler performs a transformation called Continuation-Passing Style (CPS). It adds a hidden Continuation<T> parameter to the function, which encapsulates the rest of the computation as a callback, effectively creating a state machine.

The mistakes people make

A frequent red flag is saying a suspend function "runs on a background thread." This is a dangerous oversimplification; suspension is orthogonal to threading, and a function can suspend while on the main thread. Another weak answer is simply "it's for async code," which lacks any explanatory power. Confusing suspension with thread blocking (e.g., Thread.sleep()) is a critical mistake. Finally, failing to mention the compiler's role and Continuation-Passing Style indicates a junior-level understanding.

What usually comes next

Expect questions that dig deeper into the mechanism. For example: "What exactly is a Continuation object?", "If the thread isn't blocked, what does it do while the function is suspended?", "How do Dispatchers relate to suspend functions?", or "How would you call a suspend function from Java code?".

A concrete example

Consider a function suspend fun fetchUserData(): User. When this function calls a suspending network library to get data, it doesn't block the calling thread. Instead, it suspends the coroutine. The coroutine framework saves the state (the Continuation) and returns the thread to its pool. If this was on the Android main thread, the UI remains responsive. Once the network data arrives, the framework uses the Continuation to resume the function with the result on the appropriate thread, as determined by its CoroutineDispatcher. The function picks up right where it left off, but the thread wasn't idle for the whole 500ms network call.

Interview question

What is the fundamental mechanism enabling a Kotlin suspend function to pause execution without blocking its calling thread?

  • a.It uses an internal polling mechanism to check for task completion without yielding the thread.
  • b.It's a special function type that forces the operating system to yield the CPU.
  • c.It automatically delegates the long-running task to a separate background thread.
  • d.The compiler transforms it into a state machine using Continuation-Passing Style.Correct
Why?

The correct answer is B because the card explicitly states the compiler performs a transformation called Continuation-Passing Style (CPS), which creates a state machine allowing the function to pause and resume. Option C is a common misconception; suspend functions do not inherently move execution to a background thread, as suspension is orthogonal to threading.

Just read this? Test yourself on what you have been reading.

Read the original → kotlinlang.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.

See open roles