For-in loop with ranges in Swift - Time & Space Complexity
When we use a for-in loop with ranges in Swift, we want to know how the time it takes to run changes as the range gets bigger.
We ask: How does the number of steps grow when the range increases?
Analyze the time complexity of the following code snippet.
for i in 1...n {
print(i)
}
This code prints numbers from 1 up to n using a for-in 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 gets bigger, 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 steps grows directly with n; if n doubles, steps double too.
Time Complexity: O(n)
This means the time to run 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: Even though printing is simple, the loop runs once for each number, so time grows with n.
Understanding how loops grow with input size helps you explain your code clearly and shows you know how programs scale.
"What if we changed the range to stride(from: 1, through: n, by: 2)? How would the time complexity change?"
Practice
for i in 1...3 { print(i) }Solution
Step 1: Understand the range operator
The range 1...3 is a closed range including 1, 2, and 3.Step 2: Loop prints each number in the range
The loop prints 1, then 2, then 3 on separate lines.Final Answer:
1 2 3 -> Option BQuick Check:
Closed range includes last number = 1 2 3 [OK]
- Thinking the last number is excluded
- Starting from 0 instead of 1
- Confusing closed and half-open ranges
Solution
Step 1: Identify the correct range syntax
0..<4 is a half-open range including 0,1,2,3 but excluding 4.Step 2: Check Swift for-in loop syntax
Swift uses 'for variable in range { }' syntax; 'for i in 0..<4 { }' is correct.Final Answer:
for i in 0..<4 { } -> Option CQuick Check:
Half-open range excludes last number = for i in 0..<4 { } [OK]
- Using '...' when last number should be excluded
- Using 'to' keyword which is invalid in Swift
- Using C-style for loop syntax which is not valid in Swift
var sum = 0
for number in 1..<5 {
sum += number
}
print(sum)Solution
Step 1: Identify the range and loop values
The range 1..<5 includes 1, 2, 3, 4 (excluding 5).Step 2: Calculate the sum of these numbers
Sum = 1 + 2 + 3 + 4 = 10.Final Answer:
10 -> Option AQuick Check:
Sum of 1 to 4 = 10 [OK]
- Including 5 in the sum
- Adding numbers incorrectly
- Confusing closed and half-open ranges
for i in 1..< { print(i) }Solution
Step 1: Check the range syntax
The range 1..< is incomplete; it misses the end value after '..<' operator.Step 2: Identify the error type
This causes a syntax error because Swift expects a number after '..<' to define the range.Final Answer:
Missing end value in range -> Option DQuick Check:
Range must have start and end values [OK]
- Leaving out the end value in range
- Confusing '..<' with '...' syntax
- Assuming loop variable causes error
Solution
Step 1: Understand the range and condition
We want even numbers from 2 to 10 inclusive, so use closed range 2...10.Step 2: Use 'where' clause to filter even numbers
The 'where i % 2 == 0' filters only even numbers in the loop.Final Answer:
for i in 2...10 where i % 2 == 0 { print(i) } -> Option AQuick Check:
Closed range + where filter = for i in 2...10 where i % 2 == 0 { print(i) } [OK]
- Using half-open range excluding 10
- Printing odd numbers by mistake
- Forgetting to filter even numbers
Ready to prove it?
Pass the test and this pattern moves unknown
