Skip to content
tezvyn:

What is a Kotlin extension function? Write one for String.

Source: kotlinlang.orgMediumHow cards are made

What is a Kotlin extension function? Write one for String.

Tests adding functionality to classes without inheritance. Define extension functions as static utilities, explain their use case (e.g., third-party libs), then write hasWhitespace using an idiomatic method like any. A red flag is manually looping.

What's really being asked

This question tests your practical and theoretical knowledge of a core Kotlin feature. The interviewer is checking if you understand that extension functions are syntactic sugar for static utility methods, not a form of inheritance. They are looking for a concise, idiomatic implementation, which demonstrates fluency in the Kotlin standard library, not just basic language syntax.

The full answer

First, define an extension function as a mechanism to add new functions to any class, even one you don't own (like from a third-party library or the JDK), without modifying its source code. Second, explain that they are resolved statically at compile time, not dynamically at runtime. Third, provide the correct syntax for the function signature: fun String.hasWhitespace(): Boolean. Finally, implement the logic idiomatically using a standard library function, like this.any { it.isWhitespace() }, which is far superior to a manual loop.

The mistakes people make

A major red flag is stating that extension functions modify the original class or are a form of inheritance. They do not; they are compiled down to static methods that take the receiver object as the first argument. Another common mistake is writing a verbose, C-style for loop to check for whitespace. While technically correct, it signals a lack of familiarity with idiomatic Kotlin and the powerful standard library functions available. A candidate might write for (char in this) { if (Character.isWhitespace(char)) return true } return false;. This works, but it's not the Kotlin way.

What usually comes next

Expect questions about limitations and edge cases. For instance: "What happens if a class has a member function and an extension function with the same signature?" (The member function always wins). Or, "Can you have extension properties?" (Yes, but they cannot have a backing field; they must be computed properties with a custom getter/setter). Another good follow-up is "How would you make this extension function available to your entire project?" (By defining it at the top level of any Kotlin file).

A concrete example

Here is a correct and idiomatic implementation:

fun String.hasWhitespace(): Boolean {

return this.any { it.isWhitespace() }
}

// Usage: val withSpace = "hello world" val noSpace = "helloworld"

println(withSpace.hasWhitespace()) // Prints: true println(noSpace.hasWhitespace()) // Prints: false

Interview question

Which statement accurately describes the underlying mechanism of Kotlin extension functions?

  • a.They modify the original class's bytecode to inject new methods at runtime.
  • b.They provide a mechanism for dynamic method dispatch, allowing custom implementations to override existing class methods.
  • c.They are compiled into static utility methods where the receiver object is passed as the first parameter.Correct
  • d.They enable a form of compile-time inheritance, allowing a class to implicitly extend another's functionality.
Why?

The card explicitly states that extension functions are "syntactic sugar for static utility methods" and are "compiled down to static methods that take the receiver object as the first argument." They do not modify the original class or its bytecode, which is a common misconception.

Just read this? Test yourself on what you have been reading.

Read the original → kotlinlang.org

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.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.

See open roles