Map/collect for transformation in Ruby - Time & Space Complexity
We want to understand how long it takes to change each item in a list using map or collect.
How does the time grow when the list gets bigger?
Analyze the time complexity of the following code snippet.
numbers = [1, 2, 3, 4, 5]
squares = numbers.map do |num|
num * num
end
This code takes a list of numbers and creates a new list with each number squared.
- Primary operation: The map method loops through each item in the list.
- How many times: Once for each item in the list.
Each number in the list is processed one time to create the new list.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The work grows directly with the number of items. Double the items, double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the list size.
[X] Wrong: "Map runs faster than looping because it's a special method."
[OK] Correct: Map still looks at every item one by one, so it takes about the same time as a simple loop.
Knowing how map works helps you explain how your code handles lists efficiently and clearly.
"What if we used map inside another map? How would the time complexity change?"
Practice
map (or collect) do to an array?Solution
Step 1: Understand the purpose of
Themapmapmethod takes each item in the array and applies the block's code to it.Step 2: Result of
This creates a new array with the changed items, leaving the original array unchanged.mapFinal Answer:
It creates a new array by changing each item based on the block given. -> Option CQuick Check:
map changes items and returns new array [OK]
- Thinking map changes the original array
- Confusing map with delete or sort methods
- Believing map returns a single value
[1, 2, 3] using map?Solution
Step 1: Identify correct block syntax
In Ruby,mapuses a block with{ |variable| code }ordo |variable| code end.Step 2: Check each option
[1, 2, 3].map { |x| x * 2 } uses correct block syntax with{ |x| x * 2 }. Others have syntax errors or wrong block format.Final Answer:
[1, 2, 3].map { |x| x * 2 } -> Option AQuick Check:
Use curly braces and pipe for block variables [OK]
- Missing pipes around block variable
- Using arrow syntax like JavaScript
- Incorrect do-end block without pipes
[4, 5, 6].map { |n| n + 1 }Solution
Step 1: Understand the block operation
The block adds 1 to each number in the array: 4+1=5, 5+1=6, 6+1=7.Step 2: Result of
Themapmapmethod returns a new array with these values: [5, 6, 7].Final Answer:
[5, 6, 7] -> Option DQuick Check:
Each item +1 = [5, 6, 7] [OK]
- Expecting original array to change
- Confusing map with each (which returns original array)
- Thinking map returns a single value
numbers = [1, 2, 3]
numbers.map { n n * 2 }Solution
Step 1: Check block syntax
The block is missing pipes around the variable: it should be{ |n| n * 2 }.Step 2: Understand Ruby block rules
Without pipes, Ruby does not know the block variable, causing a syntax error.Final Answer:
Missing pipe symbols around block variable -> Option BQuick Check:
Always use |var| in map blocks [OK]
- Omitting pipes in block parameters
- Thinking map is invalid on arrays
- Trying to use parentheses instead of pipes
["apple", "banana", "cherry"], how do you use map to create a new array with the length of each string?Solution
Step 1: Identify the desired transformation
We want the length of each string, so we usefruit.lengthinside the block.Step 2: Apply
Usingmapwith correct blockmap { |fruit| fruit.length }returns a new array of lengths.Final Answer:
["apple", "banana", "cherry"].map { |fruit| fruit.length } -> Option AQuick Check:
Map each string to its length [OK]
- Using unrelated methods like upcase or reverse
- Trying to get length without block variable
- Confusing map with each for transformation
Ready to prove it?
Pass the test and this pattern moves unknown
