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 your grasp of adding functionality to existing classes without inheritance. A great answer defines it as syntactic sugar over static methods, explains its use for third-party code, then implements the requested String.hasWhitespace function.

What's really being asked

This question tests your practical knowledge of a key Kotlin feature. The interviewer is looking for more than a textbook definition. They want to see that you understand how extension functions work under the hood (static dispatch), why they are used (extending final classes or third-party code), and that you can write a clean, idiomatic one on the spot. It's a test of both conceptual understanding and practical coding fluency in Kotlin.

The full answer

A strong answer has two parts. First, the definition: explain that an extension function allows you to add new functions to any existing class without inheriting from it. Crucially, state that this does not actually modify the original class; it's syntactic sugar. Under the hood, the Kotlin compiler generates a static Java method that takes the receiver object (the instance the function is called on) as its first parameter. Second, the implementation: write the function correctly. A concise, idiomatic implementation is best.

The mistakes people make

The biggest red flag is stating that extension functions modify the original class. They don't. They are resolved statically at compile time, not dynamically at runtime like virtual methods in inheritance. This means you cannot override an extension function. Another mistake is writing a verbose implementation for hasWhitespace, like using a manual for-loop and if-check, instead of using a built-in Kotlin standard library function like any. This suggests a lack of familiarity with idiomatic Kotlin. Finally, some candidates just write the code without explaining the concept, which misses half the question.

What usually comes next

Expect questions about the limitations. For example, "Can an extension function access private or protected members of the class it extends?" (No, it can only access public members). Or, "What happens if a class has a member function and you define an extension function with the same signature?" (The member function always wins). Another good one is, "What's the difference between an extension function and the Decorator pattern?" (Extensions are compile-time and simpler; Decorator is a runtime pattern providing more flexibility via object composition).

A concrete example

Here is a correct and idiomatic implementation: fun String.hasWhitespace(): Boolean = this.any { it.isWhitespace() } This is a single-expression function. It uses the any standard library function, which iterates through the characters of the String (this) and returns true as soon as the provided lambda ({ it.isWhitespace() }) returns true for any character. It's concise and leverages the power of the Kotlin standard library.

Interview question

If a class has a member function and an extension function is defined with the same signature, which one will be invoked when called on an instance of that class?

  • a.The extension function, as it is resolved statically at the call site.
  • b.The member function, because member functions always take precedence.Correct
  • c.Whichever function is brought into scope last via an import statement.
  • d.A compile-time error will occur due to the ambiguous call.
Why?

When a member function and an extension function have the same signature, the member function will always be chosen. This is because extension functions are resolved statically and do not override class members, making the member function the more specific choice.

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