Kotlin collections mirror Java lists but add read-only vs mutable interfaces: List vs MutableList. Arrays use Array<T> or primitive arrays like IntArray when you need fixed-size performance.
Lists and arrays
val names = listOf("Ada", "Bob")
val mutable = mutableListOf(1, 2, 3)
val arr = intArrayOf(10, 20, 30)
listOf returns an immutable list; mutableListOf allows add/remove. Prefer lists over arrays unless interoping with Java APIs that require arrays.
Common operations
map,filter,forEach,sumOf- Indexing:
names[0],first(),last() - Ranges:
names.slice(0..1)
Important interview questions and answers
- Q: List vs MutableList?
A: Read-only interface prevents accidental mutation at compile time; same backing list may still be mutable if cast unsafely. - Q: Array vs IntArray?
A:IntArrayavoids boxed integers;Array<Int>boxes on JVM.
Self-check
- Which factory creates an immutable list?
- When prefer
IntArrayoverArray<Int>?
Pitfall: Do not confuse Array and List at APIs—prefer List in public signatures.
Interview prep
- listOf vs mutableListOf?
listOf returns read-only interface; mutableListOf allows mutation.
- Array vs List?
Prefer List in APIs; Array is fixed-size and Java-interop heavy.