Mutable vs immutable interfaces in Kotlin - Performance Comparison
When working with mutable and immutable interfaces, it's important to understand how operations on these interfaces grow as data size increases.
We want to see how the choice between mutable and immutable affects the speed of common operations.
Analyze the time complexity of the following Kotlin code using mutable and immutable lists.
fun processImmutableList(list: List): Int {
var sum = 0
for (item in list) {
sum += item
}
return sum
}
fun processMutableList(list: MutableList): Int {
var sum = 0
for (item in list) {
sum += item
}
return sum
}
This code sums all items in either an immutable or mutable list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the list once.
- How many times: Exactly once for each element in the list.
As the list gets bigger, the number of steps grows directly with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows evenly as the list size grows.
Time Complexity: O(n)
This means the time to sum the list grows in a straight line with the number of items.
[X] Wrong: "Immutable lists are always slower because they can't change."
[OK] Correct: Both mutable and immutable lists can be traversed in the same time because reading items doesn't require changing them.
Understanding how mutable and immutable interfaces affect operation speed helps you explain your choices clearly and confidently in real projects and interviews.
"What if we added an operation that modifies the list inside the loop? How would the time complexity change for mutable vs immutable lists?"
Practice
Solution
Step 1: Understand interface roles
Immutable interfaces provide read-only access to data, preventing changes.Step 2: Compare mutable interfaces
Mutable interfaces extend immutable ones and allow data to be changed.Final Answer:
Immutable interfaces allow only reading data, while mutable interfaces allow data modification. -> Option DQuick Check:
Immutable = read-only, Mutable = read-write [OK]
- Confusing which interface allows modification
- Thinking both interfaces allow changes
- Assuming immutable interfaces are only for primitives
Solution
Step 1: Identify mutable list declaration
MutableList is the interface for mutable lists, and mutableListOf() creates a mutable list instance.Step 2: Check each option
val list: MutableList<Int> = mutableListOf(1, 2, 3) correctly declares a MutableList and initializes it with mutableListOf().Final Answer:
val list: MutableList<Int> = mutableListOf(1, 2, 3) -> Option CQuick Check:
MutableList + mutableListOf() = mutable list [OK]
- Using listOf() with MutableList type
- Declaring mutable list as List type
- Mixing mutable and immutable functions
val immutableList: List<Int> = listOf(1, 2, 3) val mutableList: MutableList<Int> = mutableListOf(4, 5, 6) println(immutableList.size) mutableList.add(7) println(mutableList)
Solution
Step 1: Check immutable list size
immutableList has 3 elements, so size is 3.Step 2: Add element to mutable list and print
mutableList adds 7, so it becomes [4, 5, 6, 7].Final Answer:
3 [4, 5, 6, 7] -> Option AQuick Check:
Immutable size 3, mutable list adds 7 [OK]
- Trying to add to immutable list causing error
- Expecting immutable list size to change
- Confusing output format
val numbers: List<Int> = listOf(1, 2, 3) numbers.add(4)
Solution
Step 1: Understand List immutability
List interface in Kotlin is immutable; it does not support add() method.Step 2: Analyze the error
Calling add(4) on immutable List causes a compilation error.Final Answer:
Cannot add to a List because it is immutable -> Option AQuick Check:
Immutable List blocks add() method [OK]
- Trying to add to immutable List
- Assuming var allows modification of immutable List
- Confusing syntax error with immutability error
List<String> parameter but you want to modify the list inside the function. Which is the best approach to handle this in Kotlin?Solution
Step 1: Understand parameter immutability
List<String> is immutable, so it cannot be modified directly.Step 2: Evaluate safe modification methods
Casting to MutableList is unsafe and may cause runtime errors; changing parameter type affects API design.Step 3: Use safe copy for modification
Creating a mutable copy inside the function allows safe modifications without affecting original list.Final Answer:
Create a new mutable copy of the list inside the function and modify that. -> Option BQuick Check:
Copy immutable list to mutable for safe changes [OK]
- Casting immutable list to mutable unsafely
- Changing parameter type breaking API contracts
- Assuming val allows modification
Ready to prove it?
Pass the test and this pattern moves unknown
