What is an extension function? Write a hasWhitespace extension for String.

Kotlin extensions are static dispatch, not true members.
Explain receiverType.functionName adds behavior without inheritance; write hasWhitespace with any { it.isWhitespace() }.
Extensions modify classes or override.
What's really being asked
This question probes whether you understand that Kotlin extension functions are syntactic sugar for static utility methods rather than true class members. The interviewer cares if you know the dispatch model, scoping rules, and receiver syntax, and whether you can write a concise, idiomatic implementation instead of a verbose Java-style helper.
The full answer
A strong response hits four things in order. First, define an extension function as a way to add new behavior to an existing class or interface without inheritance, decorators, or modifying the original source. Second, explain the syntax: prefix the function name with the receiver type and a dot, as in fun String.hasWhitespace(). Third, clarify static dispatch: extensions are resolved at compile time based on the declared type of the receiver, not the runtime type, so they cannot be overridden polymorphically and do not appear in the class bytecode as real members. Fourth, provide the implementation using the receiver this implicitly or explicitly, such as fun String.hasWhitespace(): Boolean = this.any { it.isWhitespace() }, and mention that any is a standard library extension already available on CharSequence.
The mistakes people make
The biggest red flag is saying the extension modifies the String class or that it becomes a real member available to Java callers as an instance method. Another mistake is claiming extensions participate in inheritance or can override a superclass method. Some candidates write imperative loops or manual regex instead of any, which works but misses idiomatic Kotlin. A subtle error is forgetting the return type or using contains(" ") which only catches a space and not tabs or newlines.
What usually comes next
The interviewer may ask how extensions interact with Java interop, specifically that they compile to static methods in a generated class file taking the receiver as the first argument. They may ask about nullable receivers, extension versus member function precedence when both exist, or how to scope an extension to a specific file or package. Another follow-up is asking for a generic extension, such as fun T.log(), to test generic type parameter placement.
A concrete example
If you call "Hello World".hasWhitespace(), the compiler desugars it to something like StringExtensionsKt.hasWhitespace("Hello World") in the bytecode. Inside the function, this refers to "Hello World". If you had a local variable val s: String? = "A", you could define fun String?.hasWhitespace(): Boolean = this?.any { it.isWhitespace() } ?: false, showing a nullable receiver.
Interview question
Which statement accurately describes how Kotlin resolves an extension function call on a receiver?
- a.It requires the receiver class to be open or the extension to be marked override to function
- b.It inserts the function directly into the receiver class bytecode as a new instance method
- c.It uses the compile-time declared type of the receiver, compiling down to a static method callCorrect
- d.It uses the runtime type of the receiver to choose among extension overloads, similar to virtual methods
Why? this is the answer
Kotlin extension functions are resolved at compile time based on the declared receiver type and compile to static methods, not real class members. Option D is tempting because it mirrors true polymorphic dispatch, but extensions are statically dispatched and cannot be overridden.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
- #kotlin
- #extension-functions
- #static-dispatch
- #interoperability
- #idiomatic-kotlin
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