What is a `suspend` function in Kotlin?

Tests your grasp of Kotlin's core concurrency primitive. A suspend function can pause and resume. It must be called from another suspend function or a coroutine builder.
What's really being asked
This question assesses your grasp of Kotlin Coroutines' foundational concept. The interviewer wants to see that you understand that suspend is a language-level feature for writing asynchronous code sequentially, and that you can differentiate it from actual threading. For a senior role, they expect you to know how this is achieved at a high level by the compiler.
The full answer
A strong answer will cover three key points. First, define a suspend function as a function that can have its execution paused at a suspension point and resumed later. It does not block the thread it's running on; it only suspends the coroutine. Second, state the two core rules for calling it: you can only call a suspend function from another suspend function or from within a coroutine builder (like launch, async, or runBlocking). Third, explain the "under the hood" mechanism. The Kotlin compiler performs a transformation using Continuation-Passing Style (CPS). It adds an extra, hidden Continuation<T> parameter to the function's signature. This continuation object is essentially a callback with state, holding the information needed to resume the function from where it left off.
The mistakes people make
A major red flag is equating suspend with background execution. Saying "a suspend function runs on a background thread" is incorrect. Suspension pauses the coroutine, freeing up the thread for other work, but it doesn't inherently switch threads. Thread switching is the job of the CoroutineDispatcher provided in the CoroutineContext. Another mistake is thinking suspend is just syntactic sugar for callbacks without understanding the state machine transformation performed by the compiler.
What usually comes next
Be prepared for questions like: "How does a suspend function differ from a regular blocking function?", "What is a Continuation and what role does it play?", "Explain the role of a CoroutineDispatcher and how it relates to suspend functions.", or "What is structured concurrency?".
A concrete example
A function like suspend fun fetchData(): String might make a network request. The suspend keyword allows the function to be paused without blocking its calling thread while waiting for the network I/O. Under the hood, the compiler transforms suspend fun fetchData(): String into something like fun fetchData(continuation: Continuation<String>): Any?. When an asynchronous operation starts (e.g., a delay() or a network call), the function returns a special COROUTINE_SUSPENDED marker. The continuation object saves the execution state. When the data arrives, the dispatcher uses the continuation to resume the function right where it left off, with its local variables intact.
Interview question
What is the primary effect of a `suspend` function when it needs to wait for a long-running operation like a network request?
- a.It immediately returns a callback which must be handled to get the result of the operation.
- b.It can pause the coroutine's execution, freeing the underlying thread to perform other work.Correct
- c.It automatically moves its execution to a background thread to avoid blocking the caller.
- d.It blocks the calling thread until the operation completes, guaranteeing sequential execution.
Why? this is the answer
A suspend function pauses the coroutine, freeing the thread it was running on for other work. It does not inherently switch threads (that's a dispatcher's job) or block the thread.
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.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles