Range operators (... and ..<) in Swift - Time & Space Complexity
When using Swift's range operators, it is important to know how the number of steps grows as the range size increases.
We want to understand how the time to process a range changes when the range gets bigger.
Analyze the time complexity of the following code snippet.
let n = 100
for i in 0...n {
print(i)
}
This code prints numbers from 0 up to n using the closed range operator (...).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs once for each number in the range.
- How many times: It runs n + 1 times because the range includes both 0 and n.
As n grows, the number of times the loop runs grows almost the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 11 |
| 100 | 101 |
| 1000 | 1001 |
Pattern observation: The operations increase roughly one by one as n increases.
Time Complexity: O(n)
This means the time to run the loop grows in direct proportion to the size of the range.
[X] Wrong: "The loop runs a fixed number of times no matter how big n is."
[OK] Correct: The loop runs once for each number in the range, so if n grows, the loop runs more times.
Understanding how loops over ranges grow helps you explain how your code handles bigger inputs clearly and confidently.
"What if we changed the closed range operator (...) to the half-open range operator (..<)? How would the time complexity change?"
Practice
1...5 represent?Solution
Step 1: Understand the closed range operator
The...operator creates a range that includes both the start and end values.Step 2: Apply to the given range
For1...5, it includes 1, 2, 3, 4, and 5.Final Answer:
A range including numbers 1, 2, 3, 4, and 5 -> Option AQuick Check:
1...5includes 5 [OK]
- Thinking ... excludes the last number
- Confusing ... with ..<
- Assuming ..< includes the end number
Solution
Step 1: Identify half-open range syntax
The half-open range operator in Swift is..<, which includes the start but excludes the end.Step 2: Apply to the range 0 to 9
To include numbers 0 through 9, use0..<10because 10 is excluded.Final Answer:
0..<10 -> Option BQuick Check:
..<excludes end [OK]
- Using ... when half-open range is needed
- Writing 0..9 which is invalid syntax
- Confusing end number inclusion
for i in 3..<6 {
print(i, terminator: ",")
}Solution
Step 1: Understand the range used in the loop
The range3..<6includes 3, 4, and 5 but excludes 6.Step 2: Trace the loop output
The loop prints 3, then 4, then 5, each followed by a comma.Final Answer:
3,4,5, -> Option CQuick Check:
..<6excludes 6 [OK]
- Including 6 in output
- Missing commas in output
- Confusing terminator usage
let range = 1..5
for num in range {
print(num)
}Solution
Step 1: Check the range operator used
The code uses1..5, which is not a valid range operator in Swift.Step 2: Identify correct range operators
Swift uses...for closed ranges and..<for half-open ranges, but not...Final Answer:
The range operator .. is invalid in Swift -> Option AQuick Check:
Invalid operator .. causes error [OK]
- Using .. instead of ... or ..<
- Assuming .. is a valid operator
- Ignoring compiler error messages
Solution
Step 1: Choose correct range excluding 15
To exclude 15, use the half-open range5..<15, which includes 5 up to 14.Step 2: Filter even numbers
The filter condition{ $0 % 2 == 0 }selects even numbers from the array.Step 3: Verify other options
let numbers = Array(5...15).filter { $0 % 2 == 0 } print(numbers) includes 15, let numbers = Array(5..15).filter { $0 % 2 == 0 } print(numbers) uses invalid operator.., and let numbers = Array(5..<14).filter { $0 % 2 == 0 } print(numbers) uses5..<14which goes only up to 13, missing the even number 14.Final Answer:
let numbers = Array(5..<15).filter { $0 % 2 == 0 } print(numbers) -> Option DQuick Check:
..< excludes end, filter selects even [OK]
- Using ... to include 15 mistakenly
- Using invalid .. operator
- Not filtering correctly for even numbers
Ready to prove it?
Pass the test and this pattern moves unknown
