Flat_map for nested flattening in Ruby - Time & Space Complexity
We want to understand how the time it takes to flatten nested lists using flat_map grows as the input gets bigger.
Specifically, how does the number of operations change when we use flat_map on nested arrays?
Analyze the time complexity of the following code snippet.
nested = [[1, 2], [3, 4], [5, 6]]
flat = nested.flat_map { |sub_array| sub_array.map { |x| x * 2 } }
This code doubles each number inside nested arrays and flattens the result into one array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Iterating over each sub-array and then each element inside it.
- How many times: For each element in all nested arrays combined, the block runs once.
As the total number of elements inside all nested arrays grows, the work grows roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations |
| 100 | About 100 operations |
| 1000 | About 1000 operations |
Pattern observation: The operations grow linearly with the total number of elements.
Time Complexity: O(n)
This means the time to flatten and process grows directly in proportion to the total number of elements.
[X] Wrong: "Using flat_map is slower because it has to flatten, so it must be more than linear time."
[OK] Correct: The flattening happens as part of one pass over all elements, so it still only touches each element once, keeping the time linear.
Understanding how flat_map works helps you explain how nested data can be handled efficiently, a useful skill in many coding tasks.
What if we replaced flat_map with separate map and flatten calls? How would the time complexity change?
Practice
flat_map do when applied to an array of arrays?Solution
Step 1: Understand flat_map behavior
Theflat_mapmethod applies a block to each element of the array.Step 2: Flatten the mapped results
After mapping, it flattens the resulting array by one level, combining nested arrays into a single array.Final Answer:
It applies a block to each element and flattens the result by one level. -> Option CQuick Check:
flat_map = map + flatten(1) [OK]
- Thinking flat_map flattens all nested levels
- Confusing flat_map with flatten
- Assuming flat_map does not apply a block
flat_map on an array arr to double each number?Solution
Step 1: Understand flat_map block return
The block given toflat_mapshould return an array or enumerable to flatten.Step 2: Check each option's block return
arr.flat_map { |x| [x * 2] } returns an array[x * 2], which flat_map will flatten by one level, producing a flat array of doubled values.Final Answer:
arr.flat_map { |x| [x * 2] } -> Option BQuick Check:
Block must return array for flat_map flattening [OK]
- Returning a single value instead of an array in block
- Using incorrect block syntax
- Passing arguments incorrectly to flat_map
arr = [[1, 2], [3, 4]]
result = arr.flat_map { |sub| sub.map { |x| x * 2 } }
puts result.inspectSolution
Step 1: Inner map doubles each element
For each sub-array,sub.map { |x| x * 2 }creates a new array with doubled values: [2,4] and [6,8].Step 2: flat_map flattens one level
flat_map combines these arrays into a single flat array: [2, 4, 6, 8].Final Answer:
[2, 4, 6, 8] -> Option AQuick Check:
flat_map flattens mapped arrays one level [OK]
- Expecting nested arrays in output
- Confusing map with flat_map output
- Ignoring inner map doubling
arr = [1, 2, 3, 4]
result = arr.flat_map { |x| x * 2 }
puts result.inspectSolution
Step 1: Analyze block return values
The block{ |x| x * 2 }returns integers, not arrays or enumerables.Step 2: Understand flat_map flattening requirement
flat_map expects the block to return arrays or enumerables to flatten one level; returning integers causes flattening to fail or unexpected results.Final Answer:
flat_map block returns integers, not arrays, so flattening fails. -> Option DQuick Check:
Block must return arrays for flat_map flattening [OK]
- Returning single values instead of arrays in block
- Assuming flat_map works like map without flattening
- Misunderstanding flat_map usage on nested arrays
flat_map to get a flat array of all words in uppercase?
words = [['apple', 'apricot'], ['banana', 'blueberry'], ['cherry']]
# Fill in the blank:
result = words.flat_map { |group| ________ }
puts result.inspectSolution
Step 1: Map each word to uppercase inside each group
Usegroup.map { |w| w.upcase }to convert each word in the sub-array to uppercase.Step 2: Use flat_map to flatten the mapped arrays
flat_map will flatten the arrays of uppercase words into a single flat array.Final Answer:
group.map { |w| w.upcase } -> Option AQuick Check:
flat_map + map for nested uppercase flattening [OK]
- Calling upcase on array instead of strings
- Using flat_map inside block unnecessarily
- Returning non-array values from block
Ready to prove it?
Pass the test and this pattern moves unknown
