Kotlin 2.x, KSP & Collections Interview Questions
KOTLIN › Modern Kotlin
What is the difference between List and MutableList in Kotlin? Is a List truly immutable?
What a strong answer covers: List is a read-only interface with no add or remove; MutableList extends it with mutation. Read-only isn't immutable: the underlying object may be an ArrayList that someone else holds as MutableList, or you can cast it back. For real immutability, copy defensively or use kotlinx.collections.immutable (persistent lists), which also helps Compose stability.
What's the difference between collection operations on a List and on a Sequence? When would you use a sequence?
What a strong answer covers: List operations are eager: each map or filter creates a whole new intermediate list. Sequence operations are lazy and process one element through the whole chain at a time, stopping early with first or take. Use sequences for large collections or long chains with early termination; for small lists, eager is often faster because of lower overhead.
What is KSP, and why are projects moving from kapt to KSP?
What a strong answer covers: KSP (Kotlin Symbol Processing) is an API for annotation processors that reads Kotlin symbols directly. kapt has to generate Java stubs for Kotlin code so Java annotation processors can run, which is slow. KSP is up to about twice as fast, understands Kotlin features like nullability, and is supported by Room, Moshi, Hilt and Glide. kapt is in maintenance mode.
What changed with Kotlin 2.0 and the K2 compiler, and what did a migration involve for you or what would you expect?
What a strong answer covers: K2 is a rewritten compiler front end (FIR) with a unified pipeline across platforms: significantly faster compilation, smarter smart casts (across local variables, || conditions, and inside lambdas), and a basis for new language features. The Compose compiler moved into the Kotlin repo as a Gradle plugin. Migration issues: compiler plugins and kapt need K2-compatible versions, some code needs small fixes for stricter checks, and you update the Compose compiler setup.
How do you choose between map, flatMap, fold, groupBy and associateBy for a data transformation, and where do performance traps hide in chained collection operations?
What a strong answer covers: map transforms one-to-one, flatMap one-to-many and flattens, fold accumulates into a single result with an initial value, groupBy builds Map<K, List<V>>, associateBy builds Map<K, V> keeping the last item per key. Traps: repeated chains copying big lists, calling list.contains inside a loop (O(n^2); use a Set), sorting repeatedly, and boxing primitives in List<Int> (IntArray avoids it). A strong answer measures before optimising.