Interview prep · Stage 1: Kotlin and DSA basics
Kotlin lazy vs lateinit 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 the difference between lazy and lateinit?
Show answer
private lateinit var binding: ActivityCheckoutBinding
private val priceFormat by lazy { NumberFormat.getCurrencyInstance() }Junior answer
lazy is for val and runs the block on first use. lateinit is for var that you assign later.
Senior answer
lazy is a delegate: the first read runs the block and saves the value, so the property makes its own value, once. lateinit is a plain field for a non-null var that your code sets after the constructor, like a binding in onCreate, an @Inject field or a test object in @Before. lazy can't be read too early; lateinit throws UninitializedPropertyAccessException if you do.
What they're really checking: Saying both are just "late initialization". The real difference is who sets the value.
Follow-up 1
What does by do, and how would you write your own delegate?
Show answer
class BooleanPref(
private val prefs: SharedPreferences,
private val key: String,
) : ReadWriteProperty<Any?, Boolean> {
override fun getValue(thisRef: Any?, property: KProperty<*>) =
prefs.getBoolean(key, false)
override fun setValue(thisRef: Any?, property: KProperty<*>, value: Boolean) =
prefs.edit().putBoolean(key, value).apply()
}
var darkMode by BooleanPref(prefs, "dark_mode")Answer
With by, every read of the property calls the delegate's getValue() and every write calls setValue(). You implement ReadWriteProperty (or ReadOnlyProperty for a val), or write the two operator functions yourself. It's useful when many properties share the same get and set code, like reading and writing SharedPreferences. by viewModels() and by remember { } are delegates too.
What they're really checking: Thinking lateinit is a delegate. It isn't: no by, no delegate object, just a field with a null check in the getter.
Follow-up 2
Is lazy thread-safe? What do the three modes do?
Show answer
val a by lazy { load() } // SYNCHRONIZED
val b by lazy(LazyThreadSafetyMode.PUBLICATION) { load() } // may run more than once
val c by lazy(LazyThreadSafetyMode.NONE) { load() } // one thread onlyJunior answer
Yes.
Senior answer
By default, yes: SYNCHRONIZED uses double-checked locking, so the block runs once and every thread sees the same value. After the first read there's no lock, just a volatile read. PUBLICATION lets several threads run the block at the same time and keeps one result, so the block must be safe to run twice. NONE has no lock and is only safe when one thread uses it. If the block throws, nothing is saved and the next read tries again.
What they're really checking: Switching everything to NONE for speed without measuring. The lock is only taken on the first read.
Follow-up 3
Why can't you use lateinit with Int, and how do you check if a lateinit is set?
Show answer
lateinit var count: Int // compile error
var count: Int by Delegates.notNull()
if (::binding.isInitialized) { /* safe to read */ }Junior answer
Kotlin doesn't allow it for primitives. You can use isInitialized.
Senior answer
lateinit uses null in the field to mean "not set yet". An Int property compiles to a JVM int, which can't hold null, so the compiler rejects it. Use Delegates.notNull<Int>() or a default value. To check, use ::name.isInitialized, which only works where the field is reachable: the same class, an outer class, or the same file. Needing that check often means the value is optional and should be nullable.
What they're really checking: Wrapping every read in isInitialized. That's a nullable type with extra steps.
Follow-up 4
Is this lazy safe in a Fragment?
Show answer
// Keeps the first view forever
private val totalText by lazy {
requireView().findViewById<TextView>(R.id.total)
}Answer
No. A Fragment outlives its view. When the user goes to another screen and comes back, the Fragment gets a new view, but lazy keeps the TextView from the first one. Updates go to a view nobody sees, and the old view stays in memory. A lateinit set in onViewCreated fixes the wrong view but still holds the old one while the Fragment is in the back stack. A nullable binding set in onViewCreated and cleared in onDestroyView fixes both.
What they're really checking: They're checking if you know lazy lives as long as the object, not as long as the view.
Say it in 30 seconds
Show the answer
by hands a property's get and set to a delegate object. lazy is a delegate: the first read runs the block, saves the result, and later reads return it. It only works with val and is thread-safe by default with LazyThreadSafetyMode.SYNCHRONIZED. lateinit is not a delegate. It's a plain field for a non-null var that you promise to set before the first read, like a binding in onCreate or an @Inject field. Read it too early and you get UninitializedPropertyAccessException with the property name. It doesn't work with primitive or nullable types, and ::name.isInitialized checks it. Pick lazy when the object can make the value itself, lateinit when something outside hands it in. In a Fragment, don't lazy anything from the view, because the Fragment outlives its view.