Kotlin essentials interviewers actually test
Lesson 2 of 52 min read
Null safety, data and sealed classes, scope functions — and the traps inside each.
Interviewers do not test obscure Kotlin. They test four things, and they test whether you know where each one bites.
1. Null safety
String can never be null, String? might be. ?. calls only when present, ?: supplies a fallback, !! promises it is not null and crashes when you are wrong.
val length = name?.length ?: 0
user?.email?.let(::sendMail)
The trap: !! in a code review is a conversation. requireNotNull(value) { "cart was null after checkout" } crashes with a sentence that tells you what broke.
2. data class and sealed interface
A data class is a value: equality by content, copy() for the next version of state. A sealed interface is a closed set, so when over it needs no else and adding a case becomes a compile error everywhere you must handle it.
sealed interface UiState {
data object Loading : UiState
data class Success(val items: List<Item>) : UiState
data class Error(val message: String) : UiState
}
The follow-up: "why not an enum?" Because each state carries different data. An enum constant cannot hold a list.
3. Scope functions
Four of the five are easy to separate once you ask two questions: what is the receiver and what is returned.
let—it, returns the lambda result. Null checks and transforms.run—this, returns the lambda result.apply—this, returns the object. Configuration.also—it, returns the object. Logging, side effects.with—this, returns the lambda result, not an extension.
4. Immutability that is real
val list = mutableListOf(1) still allows list.add(2). val freezes the reference, not the contents. For state you expose, use List, not MutableList, and emit a new list rather than mutating the old one — otherwise Compose and DiffUtil cannot tell anything changed.
