For loop (rarely used in Ruby) - Time & Space Complexity
We want to understand how the time it takes to run a for loop in Ruby changes as the amount of data grows.
How does the number of steps grow when the loop runs over more items?
Analyze the time complexity of the following code snippet.
for i in 1..n
puts i
end
This code prints numbers from 1 up to n using a for loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for loop runs the print command once for each number.
- How many times: Exactly n times, where n is the size of the range.
As n grows, the number of print steps grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print steps |
| 100 | 100 print steps |
| 1000 | 1000 print steps |
Pattern observation: The steps increase evenly as n increases.
Time Complexity: O(n)
This means the time grows directly in proportion to the number of items we loop over.
[X] Wrong: "The for loop runs in constant time no matter how big n is."
[OK] Correct: Because the loop runs once for each number, more numbers mean more steps, so time grows with n.
Understanding how loops grow with input size helps you explain your code clearly and shows you know how programs behave as data grows.
"What if we replaced the for loop with a nested for loop inside it? How would the time complexity change?"
Practice
for loop do?for i in 1..3
puts i
endSolution
Step 1: Understand the range 1..3
The range 1..3 includes the numbers 1, 2, and 3.Step 2: Loop through each number and print
The loop prints each number on its own line usingputs.Final Answer:
Prints numbers 1, 2, and 3 each on a new line -> Option AQuick Check:
for loop with 1..3 prints 1, 2, 3 [OK]
- Thinking 1..3 excludes 3
- Confusing puts with print
- Expecting syntax error
for loop in Ruby to iterate over an array ["a", "b", "c"]?Solution
Step 1: Check the correct Ruby for loop syntax
Ruby usesfor variable in collection do ... endor withoutdo.Step 2: Validate each option
for x in ["a", "b", "c"] do puts x end uses correct syntax withfor x in array do ... end. for (x : ["a", "b", "c"]) puts x end and C use invalid syntax. for x in ["a", "b", "c"] puts x missesendto close the loop.Final Answer:
for x in ["a", "b", "c"] do puts x end -> Option CQuick Check:
Correct Ruby for loop uses 'for x in collection do ... end' [OK]
- Using colon or 'to' instead of 'in'
- Omitting the 'end' keyword
- Adding parentheses incorrectly
sum = 0
for i in 1..4
sum += i
end
puts sumSolution
Step 1: Understand the loop range and sum initialization
The loop runs from 1 to 4, and sum starts at 0.Step 2: Calculate sum after each iteration
sum = 0+1=1, then 1+2=3, then 3+3=6, then 6+4=10.Final Answer:
10 -> Option DQuick Check:
Sum of 1 to 4 is 10 [OK]
- Forgetting to include last number 4
- Starting sum at 1 instead of 0
- Syntax errors in loop
for loop:for i in 1..3
puts i
Solution
Step 1: Check loop syntax completeness
The loop starts withforbut does not have a matchingend.Step 2: Confirm other parts are correct
The range 1..3 and variableiare valid, andputsis allowed inside loops.Final Answer:
Missingendto close the loop -> Option BQuick Check:
Every for loop needs an 'end' [OK]
- Forgetting 'end' keyword
- Thinking variable must be declared first
- Believing puts is invalid inside loops
for loop that prints only even numbers from 2 to 10 inclusive. Which code snippet achieves this correctly?Solution
Step 1: Identify the range and condition for even numbers
The range 2..10 includes numbers 2 through 10. Even numbers satisfyi % 2 == 0.Step 2: Check each option's condition
for i in 2..10 do puts i if i % 2 == 0 end prints numbers in 2..10 only if even. for i in 2..10 do puts i if i % 2 == 1 end prints odd numbers. for i in 1..10 do puts i if i % 2 == 1 end prints odd numbers 1,3,5,7,9. for i in 2..10 puts i end prints all numbers without filtering.Final Answer:
for i in 2..10 do puts i if i % 2 == 0 end -> Option AQuick Check:
Filter with 'if i % 2 == 0' inside for loop [OK]
- Using wrong modulo condition for even numbers
- Not filtering numbers inside loop
- Starting range at 1 instead of 2
Ready to prove it?
Pass the test and this pattern moves unknown
