Compare and contrast Kotlin's `apply` and `let` scope functions

This tests your grasp of idiomatic Kotlin scope functions. apply uses this and returns the object, ideal for configuration. let uses it and returns the lambda result, useful for null-safe chaining.
What's really being asked
This question evaluates your ability to write clean, idiomatic Kotlin. The interviewer wants to see if you understand the specific purpose of each scope function to improve code readability and maintainability, not just to reduce character count. It's a test of your judgment on code style and fluency with the standard library.
The full answer
First, the context object: apply references the object as a lambda receiver (this), allowing you to access its members directly (e.g., setTitle("...")). let references the object as a lambda argument (it), requiring explicit use (e.g., it.moveTo("...")).
Second, the return value: This is the critical distinction. apply returns the context object itself. This is perfect for builder-style initialization and chaining. let returns the result of the lambda expression. This is ideal for executing code on a non-null object or for chaining a series of transformations where the output of one step is the input for the next.
Third, the canonical use cases: apply is for object configuration, especially after creation (e.g., Intent().apply { ... }). let is for executing a block of code on a non-null variable (e.g., user?.let { ... }) or for scoping an expression to a specific block.
The mistakes people make
Confusing the return values is the biggest red flag. Stating that apply returns the lambda result is a fundamental misunderstanding. Another common mistake is using let for simple object configuration (e.g., person.let { it.name = "A"; it.city = "B" }). While it works, it's less readable than apply and signals a lack of familiarity with Kotlin idioms. Finally, over-nesting scope functions without a clear reason suggests a tendency to write code that is clever but hard to debug.
What usually comes next
Expect questions about the other scope functions. For example, "How does also differ from apply?" (Tests it vs this when the return value is the context object). Or, "When would you use run instead of let?" (Tests the case where you need this as context but want to return a lambda result).
A concrete example
For apply, the canonical use case is configuring an object upon creation: val intent = Intent(this, MainActivity::class.java).apply { putExtra("USER_ID", 123); action = "NAVIGATE_HOME" }. For let, the classic use is a null-safe operation: val user = getUser(id); user?.let { nonNullUser -> sendAnalyticsEvent(nonNullUser.id) }. Notice let is perfect for safe calls, while apply is perfect for chaining setup methods on a new instance.
Interview question
You must convert a nullable `user` into a `Session`, logging the user's ID first. Which implementation is the most idiomatic and correct for the function body?
- a.user?.let { log(it.id) }; return Session(user!!)
- b.return user?.let { log(it.id); Session(it) }Correct
- c.return user?.apply { log(this.id); Session(this) }
- d.if (user != null) { log(user.id); return Session(user) } else { return null }
Why? this is the answer
`let` is ideal as it executes a block on a non-null object and returns the lambda's result, which is the new `Session` object. The `apply` function is a tempting but incorrect alternative because it would return the original `user` object, not the `Session`.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
- #kotlin
- #android
- #scope functions
- #api design
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