Interview prep · Stage 1: Kotlin and DSA basics

Kotlin sealed class interview questions

Mid · 5 questions · 2 min read

One opener and four harder follow-ups. Each step builds on the last, the way a real interviewer digs.

Try each question out loud before you open the answer.

Opening question

What is a sealed class, and why use one for screen state?

Show answer

Junior answer

It's a class with a fixed list of subclasses, so a when over it needs no else.

Senior answer

It's a type whose subtypes are all known at compile time. For screen state it replaces flags like isLoading + error + order, where wrong mixes are possible, with one value that is exactly Loading, Ready or Failed. The compiler then checks every when covers all of them.

What they're really checking: That you see the real benefit: impossible states can't be written, and the compiler checks every when.

Follow-up 1

You add a Cancelled state. Which function still compiles?

Show answer
sealed interface OrderUiState {
    data object Loading : OrderUiState
    data class Ready(val total: Int) : OrderUiState
    data class Failed(val message: String) : OrderUiState
    data class Cancelled(val reason: String) : OrderUiState  // new
}

fun titleA(state: OrderUiState) = when (state) {
    is OrderUiState.Ready -> "Your order"
    is OrderUiState.Failed -> "Something went wrong"
    else -> "Loading"
}

fun titleB(state: OrderUiState) = when (state) {
    OrderUiState.Loading -> "Loading"
    is OrderUiState.Ready -> "Your order"
    is OrderUiState.Failed -> "Something went wrong"
}

Answer

titleA still compiles, and a cancelled order now shows "Loading". titleB fails with "'when' expression must be exhaustive. Add the 'is Cancelled' branch or an 'else' branch." That error is what you want: it lists every place that needs a decision.

What they're really checking: Seeing that else on a sealed type switches off the exhaustiveness check.

Follow-up 2

Where can the direct subclasses of a sealed class live?

Show answer

Junior answer

In the same file.

Senior answer

In the same module and the same package, in any file, top-level or nested. Another package fails with "A class can only extend a sealed class or interface declared in the same package"; another module fails even with the same package name. Local classes and anonymous objects can't extend it. "Same file" was the rule before Kotlin 1.5.

What they're really checking: The junior answer is the pre-1.5 rule. Knowing the module part matters in a multi-module app.

Follow-up 3

This class is in another module. Does it compile, and is the when still exhaustive?

Show answer
// :core:model
sealed class PaymentResult(val orderId: String) {
    class Paid(orderId: String, val receiptUrl: String) : PaymentResult(orderId)
    class Declined(orderId: String, val reason: String) : PaymentResult(orderId)
    open class Pending(orderId: String) : PaymentResult(orderId)
}

// :feature:checkout
class UpiPending(orderId: String) : PaymentResult.Pending(orderId)

fun label(r: PaymentResult) = when (r) {
    is PaymentResult.Paid -> "Paid"
    is PaymentResult.Declined -> r.reason
    is PaymentResult.Pending -> "Waiting for ${r.orderId}"
}

Answer

Yes to both. The same-module and same-package rule only applies to direct subclasses. Pending is open, so UpiPending can extend it from anywhere. The is PaymentResult.Pending branch also covers UpiPending. Make Pending sealed if you want that level closed too.

What they're really checking: Direct vs indirect subclasses: the closed list is only one level deep.

Follow-up 4

Sealed class or sealed interface: which would you pick, and when?

Show answer
sealed interface CartEvent
sealed interface AnalyticsEvent

data class ItemAdded(val name: String) : CartEvent, AnalyticsEvent
enum class SortOrder : CartEvent { PRICE, RATING }

Junior answer

Sealed interface for UI state, sealed class when you need a constructor.

Senior answer

A sealed class can hold constructor data and logic shared by every subtype, like orderId, but a type can extend only one class. A sealed interface has no stored state, but one type can implement several, so it can be in two closed lists, and an enum can implement it. Default to a sealed interface; use a sealed class when subtypes share constructor data.

Say it in 30 seconds

Show the answer

A sealed class or interface has a closed list of subtypes, so a when over it can be checked for every case. Direct subtypes must be in the same module and package; indirect ones can live anywhere. Avoid else on a sealed when, because it hides new cases. Use a sealed interface by default, and a sealed class when subtypes share constructor data.

How did you do?