Break, next, and redo behavior in Ruby - Time & Space Complexity
When using break, next, and redo in loops, the number of times the loop runs can change.
We want to see how these commands affect how long the code takes as the input grows.
Analyze the time complexity of the following code snippet.
arr = [1, 2, 3, 4, 5]
arr.each do |num|
redo_count ||= 0
redo_count += 1
next if num.even?
puts num
break if num >= arr.size
redo if redo_count < 2 && num == 1
end
This code loops over an array, skips even numbers, prints odd numbers, stops if the number is greater than or equal to the array size, and repeats the first number once more.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over the array elements with
each. - How many times: Normally once per element, but
redorepeats the current element, andbreakcan stop early.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times, plus any redo repeats |
| 100 | About 100 times, plus redo repeats |
| 1000 | About 1000 times, plus redo repeats |
Pattern observation: The loop mostly runs once per item, but redo can add a few repeats, and break can stop early, so time grows roughly with input size.
Time Complexity: O(n)
This means the time grows roughly in direct proportion to the number of items in the input.
[X] Wrong: "Using redo makes the loop run forever and time complexity is infinite."
[OK] Correct: Redo repeats the current step but only when triggered by a condition; it does not always cause infinite loops if used carefully.
Understanding how break, next, and redo affect loops helps you explain how your code runs and how efficient it is.
"What if we removed the break statement? How would the time complexity change?"
Practice
break keyword do inside a Ruby loop?Solution
Step 1: Understand the role of
Thebreakbreakkeyword immediately stops the loop and exits it, no further iterations happen.Step 2: Compare with other keywords
nextskips to the next iteration,redorepeats the current one, butbreakstops all looping.Final Answer:
Stops the loop completely and exits it -> Option AQuick Check:
break= stop loop [OK]
- Confusing break with next
- Thinking break repeats iteration
- Assuming break pauses instead of stops
Solution
Step 1: Identify the keyword for skipping iteration
The keywordnextis used in Ruby to skip the rest of the current loop iteration and move to the next one.Step 2: Check other options
breakstops the loop,redorepeats the current iteration, andskipis not a Ruby keyword.Final Answer:
next -> Option BQuick Check:
Skip iteration = next [OK]
- Using break instead of next
- Trying to use skip keyword (not valid)
- Confusing redo with next
i = 0
while i < 3
i += 1
if i == 2
redo
end
puts i
endSolution
Step 1: Trace the loop and redo behavior
Wheni == 2,redorepeats the current iteration without incrementingiagain, causing the loop to print 2 repeatedly.Step 2: Understand infinite repetition
Sinceiis not changed inside the redo loop, it never moves past 2, causing an infinite loop printing 2 repeatedly.Final Answer:
1 2 2 2 ... (infinite loop) -> Option AQuick Check:
redo repeats current step causing infinite loop [OK]
- Assuming i increments inside redo
- Thinking redo skips iteration
- Ignoring infinite loop possibility
for i in 1..3
if i == 2
next
elsif i == 3
redo
end
puts i
endSolution
Step 1: Analyze the use of redo in the loop
Theredoinside theelsif i == 3causes the loop to repeat the same iteration infinitely becauseidoes not change.Step 2: Check other options for errors
nextworks fine in for loops, the if statement is properly closed, andputscan be after if.Final Answer:
Using redo inside for loop causes infinite loop -> Option DQuick Check:
Redo repeats current step causing infinite loop [OK]
- Thinking next is invalid in for loops
- Missing that redo repeats without increment
- Assuming syntax error due to indentation
next and redo. Which code achieves this correctly?
i = 0
while i < 5
i += 1
if i == 3
# skip 3
elsif i == 4
# print 4 twice
end
puts i
endSolution
Step 1: Skip number 3 using next
Usingnextwheni == 3skips printing 3 and moves to next iteration.Step 2: Repeat printing 4 twice using redo
Wheni == 4, print it once, then useredoto repeat the iteration and print 4 again.Step 3: Verify code correctness
if i == 3 next elsif i == 4 puts i redo end prints 4, then calls redo to repeat printing 4, and skips 3 correctly. Other options misuse next or redo causing errors or infinite loops.Final Answer:
if i == 3 next elsif i == 4 puts i redo end -> Option CQuick Check:
next skips, redo repeats current step [OK]
- Calling redo before puts causing infinite loop
- Using next after redo incorrectly
- Not skipping 3 properly
Ready to prove it?
Pass the test and this pattern moves unknown
