Var for mutable references in Kotlin - Time & Space Complexity
Let's see how using var for mutable references affects how long a program takes to run.
We want to know how the program's steps grow when we change the input size.
Analyze the time complexity of the following code snippet.
fun sumList(numbers: List): Int {
var total = 0
for (num in numbers) {
total += num
}
return total
}
This code adds up all numbers in a list using a mutable variable total.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop that goes through each number in the list. - How many times: Once for every item in the list.
As the list gets bigger, the program does more additions, one for each number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the list size.
[X] Wrong: "Using var makes the code slower because it changes values."
[OK] Correct: Changing a variable's value itself doesn't add extra loops or steps. The main time cost is from how many times the loop runs, not from using var.
Understanding how mutable variables work helps you explain how your code runs step-by-step, which is a useful skill in interviews and real projects.
What if we replaced the for loop with a recursive function? How would the time complexity change?
Practice
var keyword do in Kotlin?Solution
Step 1: Understand the role of
Thevarvarkeyword declares a variable that can be changed after it is created.Step 2: Compare with other keywords
valcreates a constant (unchangeable) variable, sovaris for mutable variables.Final Answer:
Creates a variable whose value can be changed later -> Option CQuick Check:
varmeans mutable variable [OK]
- Confusing var with val (immutable)
- Thinking var declares constants
- Mixing var with function or class declarations
count with initial value 10 in Kotlin?Solution
Step 1: Identify mutable variable declaration
To declare a mutable variable, usevarwith an initial value.Step 2: Check syntax correctness
var count = 10correctly declares a mutable integer variable with initial value 10.Final Answer:
var count = 10 -> Option DQuick Check:
Mutable variable needsvarand initial value [OK]
- Using val instead of var for mutable variables
- Declaring var without initial value or type
- Confusing val and var syntax
var x = 5 x = 10 println(x)
Solution
Step 1: Understand variable assignment
Variablexis declared mutable with initial value 5, then changed to 10.Step 2: Print the current value of
The print statement outputs the updated value 10.xFinal Answer:
10 -> Option AQuick Check:
Mutable var allows reassignment, so output is 10 [OK]
- Thinking var variables cannot be reassigned
- Expecting initial value to print
- Confusing compilation and runtime errors
var name: String name = "Alice" name = 123
Solution
Step 1: Check variable type and assignments
Variablenameis declared as String, assigned "Alice" (valid), then assigned 123 (number).Step 2: Identify type mismatch error
Assigning an integer to a String variable causes a type error.Final Answer:
Cannot assign a number to a String variable -> Option BQuick Check:
Type mismatch causes error [OK]
- Ignoring type mismatch errors
- Thinking var requires initial value always
- Confusing val and var usage
var?Solution
Step 1: Declare a mutable variable with initial value
Usevar score = 0to allow changes to score.Step 2: Update the score correctly
Adding 10 to score withscore = score + 10works because score is mutable.Final Answer:
var score = 0 score = score + 10 println(score) -> Option AQuick Check:
Mutable var allows updating score [OK]
- Using val for variables that need updating
- Declaring var without initial value and using before assignment
- Trying to update val variables causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
