Array creation methods in Ruby - Time & Space Complexity
When we create arrays in Ruby, the time it takes can change depending on how we do it.
We want to know how the time grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
arr = Array.new(n) { |i| i * 2 }
This code creates an array of size n, filling each element with twice its index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Filling each element of the array by running the block once per element.
- How many times: Exactly
ntimes, once for each element.
As the array size n grows, the number of times the block runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The operations increase directly with the size of the array.
Time Complexity: O(n)
This means the time to create the array grows in a straight line with the number of elements.
[X] Wrong: "Creating an array with a block is instant and does not depend on size."
[OK] Correct: Each element is set by running the block, so the time grows with the number of elements.
Understanding how array creation time grows helps you write efficient code and explain your choices clearly in interviews.
"What if we create an array by repeating the same value instead of using a block? How would the time complexity change?"
Practice
Solution
Step 1: Understand Ruby array syntax
In Ruby, arrays are created using square brackets[]for literals.Step 2: Identify correct empty array syntax
[]creates an empty array, while{}creates an empty hash, and the other options are not Ruby syntax.Final Answer:
[] -> Option BQuick Check:
Empty array = [] [OK]
- Using curly braces {} which creates a hash
- Using parentheses like array() which is invalid
- Using JavaScript-like syntax new Array()
Solution
Step 1: Recall Array.new syntax
In Ruby,Array.new(size, default_value)creates an array with given size and fills it with the default value.Step 2: Check each option
Array.new(3, 0)correctly creates [0, 0, 0].Array.new(0, 3)creates an empty array since size is first argument. Options C and D are invalid syntax.Final Answer:
Array.new(3, 0) -> Option AQuick Check:
Array.new(3, 0) = [0, 0, 0] [OK]
- Confusing Array.new arguments order
- Using invalid syntax like Array(0, 3)
- Trying JavaScript style new Array()
arr = Array.new(4) { |i| i * 2 }
puts arr.inspectSolution
Step 1: Understand Array.new with block
When Array.new is called with a block, it runs the block for each index, passing the index asi.Step 2: Calculate each element
For indices 0 to 3, the block returnsi * 2: 0*2=0, 1*2=2, 2*2=4, 3*2=6.Final Answer:
[0, 2, 4, 6] -> Option CQuick Check:
Array with block fills elements by index * 2 [OK]
- Assuming default value 0 for all elements
- Confusing block argument meaning
- Expecting nil values without block
arr = Array.new(3, []) arr[0] << 1 puts arr.inspect
Solution
Step 1: Understand Array.new with default object
UsingArray.new(3, [])creates 3 references to the same empty array, not 3 separate arrays.Step 2: Analyze mutation effect
Whenarr[0] << 1runs, it adds 1 to the shared array, so all elements show the change.Final Answer:
[[1], [1], [1]] -> Option AQuick Check:
Same object repeated causes all elements to change [OK]
- Expecting separate arrays but all share one object
- Not using block form for unique elements
- Assuming << affects only one element
Solution
Step 1: Understand object references in Array.new
UsingArray.new(5, {a: 0, b: 0})or[{a: 0, b: 0}] * 5repeats the same hash object 5 times.Step 2: Use block form for unique hashes
UsingArray.new(5) { {a: 0, b: 0} }runs the block 5 times, creating 5 separate hashes.Final Answer:
Array.new(5) { {a: 0, b: 0} } -> Option DQuick Check:
Block form creates unique objects [OK]
- Using Array.new with default hash causes shared references
- Using multiplication operator with arrays of hashes
- Trying invalid Array() constructor syntax
Ready to prove it?
Pass the test and this pattern moves unknown
