Kotlin Interview Questions and Answers

Null safety, coroutines, extension functions and Java interop.

Practise 10 random 2 peer-reviewed questions
Kotlin Interview Syllabus & Preparation Strategy

Whether you are preparing for entry-level Kotlin interview questions for freshers or senior software engineer interview questions addressing concurrency, scalability, and system architecture, this track provides peer-reviewed model answers with syntax walkthroughs, edge cases, and practical interview tips.

1 How does structured concurrency and cancellation work with coroutines? Hard

A coroutine is cooperative: cancellation sets a flag that the coroutine must observe. Suspend functions from kotlinx.coroutines check automatically and throw CancellationException, but a tight loop that never suspends is not cancellable unless you check.

val job = launch {
    repeat(1000) { i ->
        ensureActive()          // or check isActive
        process(i)
    }
}
job.cancelAndJoin()

Structured concurrency means a scope cannot complete until its children do. coroutineScope waits for children and cancels siblings when one fails; supervisorScope isolates failures so one child does not cancel the others. Cancellation propagates from parent to child.

Never swallow CancellationException. If cleanup must suspend during cancellation, wrap it in withContext(NonCancellable). This model prevents leaked background work and is why launching coroutines in a scope you control matters.

2 Explain declaration-site variance with in and out. Hard

Variance describes how generic types relate when their type arguments are related. out T (covariant) means Producer<String> can be used where Producer<Any> is expected. in T (contravariant) means Consumer<Any> can be used where Consumer<String> is expected.

interface Producer<out T> { fun produce(): T }   // only returns T
interface Consumer<in T>  { fun consume(t: T) }  // only accepts T

fun feed(c: Consumer<Any>) { c.consume("hi") }
val stringConsumer: Consumer<String> = ...
feed(stringConsumer)   // safe: a String consumer handles Any

Kotlin uses declaration-site variance with out and in; Java uses use-site wildcards (? extends, ? super). The safety rule: an out parameter may only appear in return positions, and an in parameter only in parameter positions. Violating this is a compile error, which prevents unsound assignments.

Kotlin also supports use-site projections for individual calls and star projections like List<*> when the type argument is unknown. Understanding variance explains why List is covariant but MutableList is invariant.

Frequently Asked Questions About Kotlin Interviews

What do hiring managers evaluate in Kotlin technical rounds?

Technical interviewers look for foundational fluency, idiomatic syntax, clarity when communicating complex logic, and awareness of performance trade-offs (e.g. memory footprint, render performance, and network latency) in production environments.

What are the best interview tips for practicing Kotlin questions?

Use active recall: summarize each answer in your own words before revealing the model solution. Focus on explaining why a certain approach is chosen rather than just memorizing code syntax.