Stride for custom step in Swift - Time & Space Complexity
When using stride with a custom step in Swift, it is important to understand how the number of steps affects the time it takes to run the code.
We want to know how the execution time grows as the range and step size change.
Analyze the time complexity of the following code snippet.
let start = 0
let end = 100
let step = 5
for i in stride(from: start, to: end, by: step) {
print(i)
}
This code prints numbers from 0 up to but not including 100, stepping by 5 each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop using stride repeats printing.
- How many times: It repeats once for each step from start to end, increasing by the step size.
As the range between start and end grows, the number of loop steps increases proportionally to how many steps fit in that range.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 (steps of 5: 0, 5) |
| 100 | 20 (steps of 5: 0, 5, 10, ..., 95) |
| 1000 | 200 (steps of 5: 0, 5, 10, ..., 995) |
Pattern observation: The number of operations grows roughly in direct proportion to the input size divided by the step size.
Time Complexity: O(n / step)
This means the time grows linearly with the number of steps taken, which depends on the range size divided by the step size.
[X] Wrong: "The loop always runs n times regardless of the step size."
[OK] Correct: The step size controls how many times the loop runs; bigger steps mean fewer iterations.
Understanding how loops with custom steps scale helps you explain performance clearly and shows you can reason about code efficiency in real situations.
"What if we changed the step size to 1? How would the time complexity change?"
Practice
stride(from: 1, to: 10, by: 2) do?Solution
Step 1: Understand the parameters of stride
The function usesfrom: 1as the start,to: 10as the end (exclusive), andby: 2as the step size.Step 2: Determine the sequence generated
It generates numbers starting at 1, adding 2 each time, stopping before reaching 10, so the sequence is 1, 3, 5, 7, 9.Final Answer:
Generates numbers starting at 1, increasing by 2, up to but not including 10. -> Option CQuick Check:
stride(from:1, to:10, by:2) excludes 10 [OK]
- Confusing 'to' with 'through' and including the end value
- Mixing up positive and negative step directions
- Assuming the step is always 1
Solution
Step 1: Identify counting down with inclusion
Counting down from 10 to 2 means the step is negative (-2). Including 2 means usingthrough.Step 2: Match syntax to requirements
Onlystride(from: 10, through: 2, by: -2)matches counting down with inclusion of 2.Final Answer:
stride(from: 10, through: 2, by: -2) -> Option AQuick Check:
Use 'through' to include end, negative step for countdown [OK]
- Using 'to' instead of 'through' when including the end
- Using positive step when counting down
- Swapping start and end values
for i in stride(from: 0, to: 5, by: 2) {
print(i)
}Solution
Step 1: Analyze the stride parameters
The loop starts at 0, goes up to but excludes 5, stepping by 2.Step 2: List the values printed
Values are 0, 2, 4. It stops before reaching 5.Final Answer:
0 2 4 -> Option AQuick Check:
Stride 'to' excludes end, so 5 is not printed [OK]
- Including the end value 5 mistakenly
- Adding an extra step beyond the end
- Confusing step size with end value
for i in stride(from: 1, through: 10, by: 0) {
print(i)
}Solution
Step 1: Check the step parameter
The step is zero, which is invalid because it causes no progress in the loop.Step 2: Understand the effect of zero step
A zero step causes a runtime error or infinite loop because the loop never advances.Final Answer:
Step value cannot be zero; it causes a runtime error. -> Option BQuick Check:
Step must not be zero in stride [OK]
- Thinking 'through' is invalid syntax
- Assuming zero step means step of 1
- Ignoring runtime errors caused by zero step
Solution
Step 1: Identify stride to generate odd numbers 1 to 15 inclusive
Usingstride(from: 1, through: 15, by: 2)generates 1,3,5,...,15 including 15.Step 2: Filter numbers greater than 5
The conditionif i > 5prints only numbers greater than 5, i.e., 7,9,11,13,15.Final Answer:
for i in stride(from: 1, through: 15, by: 2) { if i > 5 { print(i) } } -> Option DQuick Check:
Stride with 'through' includes 15; condition filters >5 [OK]
- Using 'to' excludes 15, missing last number
- Incorrect condition filtering numbers equal to 5
- Starting stride at 5 misses odd numbers less than 5
Ready to prove it?
Pass the test and this pattern moves unknown
