For loop with ranges in Kotlin - Time & Space Complexity
We want to understand how the time it takes to run a for loop with ranges changes as the range size grows.
How does the number of steps grow when the range gets bigger?
Analyze the time complexity of the following code snippet.
for (i in 1..n) {
println(i)
}
This code prints numbers from 1 up to n using a for loop with a range.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs once for each number from 1 to n.
- How many times: Exactly n times, where n is the size of the range.
As n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of operations grows directly with n. If n doubles, the work doubles.
Time Complexity: O(n)
This means the time to run the loop grows in a straight line with the size of the range.
[X] Wrong: "The loop runs in constant time because it just prints numbers."
[OK] Correct: Each print takes time, and since the loop runs n times, the total time grows with n.
Understanding how loops grow with input size helps you explain code efficiency clearly and confidently in interviews.
"What if we changed the loop to run from 1 to n squared (1..n*n)? How would the time complexity change?"
Practice
for (i in 1..5) { println(i) }Solution
Step 1: Understand the range operator
The operator1..5creates a range including numbers 1, 2, 3, 4, and 5.Step 2: Loop through the range
The for loop runs from 1 to 5, printing each number.Final Answer:
Prints numbers from 1 to 5 inclusive -> Option BQuick Check:
Range 1..5 includes 5 [OK]
- Thinking .. excludes the last number
- Confusing range with step or downTo
- Assuming it counts down automatically
Solution
Step 1: Identify correct descending range syntax
Kotlin usesdownToto count down from a higher number to a lower number.Step 2: Check each option
for (i in 5 downTo 1) { println(i) } uses5 downTo 1, which is correct. for (i in 5..1) { println(i) } uses an invalid range (5..1). for (i in 1..5 step -1) { println(i) } tries to use negative step incorrectly. for (i in 1 downTo 5) { println(i) } reverses the range incorrectly.Final Answer:
for (i in 5 downTo 1) { println(i) } -> Option AQuick Check:
Use downTo for descending loops [OK]
- Using .. for descending ranges
- Trying negative step with .. (not allowed)
- Swapping start and end in downTo
var sum = 0
for (i in 1..5 step 2) {
sum += i
}
println(sum)Solution
Step 1: Understand the loop range and step
The loop runs from 1 to 5, stepping by 2: values are 1, 3, 5.Step 2: Calculate the sum of these values
sum = 0 + 1 + 3 + 5 = 9Final Answer:
9 -> Option CQuick Check:
1 + 3 + 5 = 9 [OK]
- Adding all numbers 1 to 5 instead of stepping
- Using step as a multiplier
- Confusing step with downTo
for (i in 10..1 step 2) {
println(i)
}Solution
Step 1: Analyze the range 10..1
The range 10..1 tries to count up from 10 to 1, which is invalid because 10 is greater than 1.Step 2: Understand correct descending syntax
To count down, usedownToinstead of .., e.g.,10 downTo 1 step 2.Final Answer:
Range 10..1 is invalid for counting down -> Option DQuick Check:
Use downTo for descending ranges [OK]
- Using .. for descending ranges
- Thinking step fixes descending range
- Ignoring Kotlin's range direction rules
Solution
Step 1: Identify the range and step for even numbers
Even numbers from 2 to 20 can be generated by starting at 2 and stepping by 2 up to 20.Step 2: Check each option
for (i in 2..20 step 2) { println(i) } uses2..20 step 2, which prints 2,4,6,...,20 correctly. for (i in 2..19) { if (i % 2 == 0) println(i) } ends at 19 so prints evens up to 18, missing 20. for (i in 2 downTo 20 step 2) { println(i) } counts down incorrectly. for (i in 1..10 step 2) { println(i * 2) } prints 2,6,10,..., not all even numbers.Final Answer:
for (i in 2..20 step 2) { println(i) } -> Option AQuick Check:
Use step 2 from 2 to 20 for evens [OK]
- Using downTo instead of .. for ascending
- Forgetting to step by 2
- Multiplying inside loop incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
