Array modification (push, pop, shift, unshift) in Ruby - Time & Space Complexity
When we add or remove items from an array, the time it takes can change depending on how we do it.
We want to know how the work grows when we use push, pop, shift, or unshift on arrays.
Analyze the time complexity of the following code snippet.
arr = []
arr.push(10) # Add item at the end
arr.pop # Remove item from the end
arr.unshift(5) # Add item at the start
arr.shift # Remove item from the start
This code shows adding and removing items at both ends of an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding or removing one item at either end of the array.
- How many times: Each operation happens once, but internally some may move many items.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations for push/pop | Approx. Operations for shift/unshift |
|---|---|---|
| 10 | 1 (simple add/remove) | 10 (move all items) |
| 100 | 1 (simple add/remove) | 100 (move all items) |
| 1000 | 1 (simple add/remove) | 1000 (move all items) |
Pattern observation: push and pop take about the same small time no matter the size, but shift and unshift take longer as the array grows.
Time Complexity: O(1) for push/pop, O(n) for shift/unshift
This means adding or removing at the end is fast and steady, but doing it at the start gets slower as the array gets bigger.
[X] Wrong: "All array add/remove operations take the same time."
[OK] Correct: Adding or removing at the start needs to move all other items, so it takes longer when the array is big.
Understanding these differences helps you explain why some array operations are faster, showing you know how data structures work behind the scenes.
"What if we used a linked list instead of an array? How would the time complexity for adding or removing at the start change?"
Practice
Solution
Step 1: Understand the purpose of
Thepushpushmethod adds an element to the end of an array.Step 2: Compare with other methods
popremoves the last element,shiftremoves the first, andunshiftadds to the front.Final Answer:
push -> Option CQuick Check:
push adds to end [OK]
- Confusing push with pop
- Mixing shift and unshift
- Thinking pop adds elements
arr in Ruby?Solution
Step 1: Identify method to remove first element
Theshiftmethod removes and returns the first element of an array.Step 2: Check syntax correctness
Callingarr.shift()correctly removes the first element.Final Answer:
arr.shift() -> Option AQuick Check:
shift removes first element [OK]
- Using pop to remove first element
- Calling unshift to remove elements
- Forgetting parentheses
arr = [1, 2, 3] arr.push(4) arr.shift p arr
Solution
Step 1: Execute
Adding 4 to the end changes array from [1, 2, 3] to [1, 2, 3, 4].push(4)on arrayStep 2: Execute
Removing first element (1) results in [2, 3, 4].shiftto remove first elementFinal Answer:
[2, 3, 4] -> Option DQuick Check:
push adds end, shift removes front = [2, 3, 4] [OK]
- Ignoring shift effect
- Thinking push adds front
- Confusing pop with shift
nums and then remove the last element. What is the error?nums = [1, 2, 3] nums.unshift(0) nums.pop()
Solution
Step 1: Understand
This adds 0 to the front, changing [1, 2, 3] to [0, 1, 2, 3].unshift(0)effectStep 2: Understand
This removes the last element (3), resulting in [0, 1, 2].pop()effectFinal Answer:
No error; code works correctly -> Option AQuick Check:
unshift adds front, pop removes last = correct [OK]
- Thinking pop removes first element
- Believing unshift does not add
- Swapping order of operations unnecessarily
data = [10, 20, 30], which sequence of method calls will result in [5, 10, 20]?Solution
Step 1: Apply
Array changes from [10, 20, 30] to [5, 10, 20, 30].unshift(5)to add 5 at frontStep 2: Apply
Removing last element (30) results in [5, 10, 20].pop()to remove last elementFinal Answer:
data.unshift(5).pop() -> Option BQuick Check:
unshift adds front, pop removes last = [5, 10, 20] [OK]
- Using push instead of unshift
- Removing first element instead of last
- Chaining methods incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
