Interview prep · Stage 1: Kotlin and DSA basics
Kotlin backing field vs backing property interview questions
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 backing field in Kotlin?
Show answer
var volume: Int = 50
set(value) {
field = value.coerceIn(0, 100)
}
val isMuted: Boolean
get() = volume == 0 // no backing fieldJunior answer
It's a variable called field that every property has.
Senior answer
It's the storage the compiler creates for a property when it needs to store a value: when the property uses the default getter or setter, or when a custom getter or setter uses the field keyword. field is only visible inside that property's getter and setter. A property that only computes its value, like val isMuted get() = volume == 0, has no backing field, and can't have an initializer.
What they're really checking: Saying every property has a backing field. Computed properties don't.
Follow-up 1
This setter crashes the first time it runs. Why?
Show answer
var volume: Int = 50
set(value) {
volume = value.coerceIn(0, 100) // calls itself
}Answer
Inside the setter, volume = ... doesn't write the value. It calls the setter again, which calls it again, until the stack runs out with StackOverflowError. Writing to field skips the setter and stores the value directly. The same happens with get() = volume in a getter.
What they're really checking: Thinking the property name inside its own accessor means the stored value.
Follow-up 2
What is a backing property, and why do ViewModels use _uiState?
Show answer
private val _uiState = MutableStateFlow(UiState())
val uiState: StateFlow<UiState> = _uiState.asStateFlow()Junior answer
It's a private copy of the property, named with an underscore.
Senior answer
It's a second, private property you write yourself to hold the real value when the public type should be different. A backing field always has the property's type, so it can't do this. The ViewModel keeps private val _uiState = MutableStateFlow(UiState()) so only it can change the state, and exposes val uiState: StateFlow<UiState> = _uiState.asStateFlow() so the screen can only read. The underscore is just the naming convention.
What they're really checking: Calling _uiState a copy. It's the same state; uiState is a read-only view of it.
Follow-up 3
Why asStateFlow() and not just val uiState: StateFlow<UiState> = _uiState?
Show answer
(viewModel.uiState as MutableStateFlow).value = UiState()
// = _uiState -> changes the state
// = _uiState.asStateFlow() -> ClassCastExceptionAnswer
With = _uiState, the public property is the same MutableStateFlow object. The type says StateFlow, but a caller can cast it back to MutableStateFlow and set values. asStateFlow() returns a read-only wrapper (ReadonlyStateFlow), so the same cast throws ClassCastException. The cost is one small extra object and one extra field.
What they're really checking: Believing the declared type alone protects the state at runtime.
Follow-up 4
What are explicit backing fields in Kotlin 2.4, and would you replace _uiState with them?
Show answer
val uiState: StateFlow<UiState>
field = MutableStateFlow(UiState())
fun onSearch(query: String) {
uiState.update { it.copy(query = query) } // smart cast
}Answer
Kotlin 2.4.0 made them Stable. You give the property a public type and its field a more specific type: val uiState: StateFlow<UiState> field = MutableStateFlow(UiState()). Inside the class, the compiler smart casts uiState to MutableStateFlow, so update {} works. Outside, callers see StateFlow. Rules: val only, final, no custom getter, field type a subtype of the property type, field always private. I'd use it in new code on Kotlin 2.4+, but the getter returns the field itself, so it doesn't block the cast the way asStateFlow() does. If that matters, keep the backing property.
What they're really checking: Trying it on a var or with a custom getter. The compiler rejects both.
Say it in 30 seconds
Show the answer
A backing field is the storage Kotlin creates for a property. It exists when the property uses a default getter or setter, or when a custom one uses field, and field is only visible inside that property's getter and setter. A computed property like val isMuted get() = volume == 0 has no backing field. Inside a setter you write field = value, because volume = value calls the setter again and ends in StackOverflowError. A backing property is a second, private property you write yourself, like _uiState: MutableStateFlow, when the stored type should differ from the public one, uiState: StateFlow. Use asStateFlow() so callers can't cast it back. Since Kotlin 2.4, an explicit backing field does the same in one val: val uiState: StateFlow<UiState> field = MutableStateFlow(UiState()). It only works for final vals with no custom getter, and the getter returns the field itself, so it doesn't block a cast like asStateFlow() does.