Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Kotlin
Kotlinmedium

Extension functions — what are they and what is a common pitfall?

Tags
#extensions#dispatch#kotlin-basics
Back to categoryPractice quiz

Answer

They let you add functions to a type without modifying its source (syntactic sugar). A common pitfall: extensions are resolved statically by the declared type, so they don’t behave like virtual overrides.

open class Base
class Child : Base()

fun Base.say() = "base"
fun Child.say() = "child"

val x: Base = Child()
println(x.say()) // "base" (static dispatch)

Advanced answer

Deep dive

Expanding on the short answer — what usually matters in practice:

  • Context (tags): extensions, dispatch, kotlin-basics
  • JVM: memory (heap/stack), GC, and what drives latency.
  • Contracts: equals/hashCode/toString, mutability and consequences.
  • Performance: boxing, allocations, collections, inlining.
  • Explain the "why", not just the "what" (intuition + consequences).
  • Trade-offs: what you gain/lose (time, memory, complexity, risk).
  • Edge cases: empty inputs, large inputs, invalid inputs, concurrency.

Examples

Here’s an additional example (building on the short answer):

open class Base
class Child : Base()

fun Base.say() = "base"
fun Child.say() = "child"

val x: Base = Child()
println(x.say()) // "base" (static dispatch)

Common pitfalls

  • Too generic: no concrete trade-offs or examples.
  • Mixing average-case and worst-case (e.g., complexity).
  • Ignoring constraints: memory, concurrency, network/disk costs.

Interview follow-ups

  • When would you choose an alternative and why?

Related questions

Kotlin
Extension functions: how are they dispatched and what is a common pitfall?
#kotlin#extension#dispatch
What production issues show up and how do you diagnose them?
  • How would you test edge cases?