Collection functions (length, flatten, merge) in Terraform - Time & Space Complexity
We want to understand how the time it takes to run collection functions changes as the size of the collections grows.
How does the work grow when we use length, flatten, or merge on bigger lists or maps?
Analyze the time complexity of the following code snippet.
locals {
list_of_lists = [[1, 2], [3, 4], [5, 6]]
merged_map = merge({a = 1}, {b = 2}, {c = 3})
flat_list = flatten(local.list_of_lists)
list_length = length(local.flat_list)
}
This code merges maps, flattens a list of lists into a single list, and finds the length of the flattened list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing each element in the lists or maps to merge or flatten.
- How many times: Each element is visited once during flatten or merge.
As the number of elements in the collections grows, the work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to process all elements |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: Doubling the input roughly doubles the work needed.
Time Complexity: O(n)
This means the time to run these functions grows linearly with the size of the input collections.
[X] Wrong: "Flatten or merge runs instantly no matter how big the input is."
[OK] Correct: These functions must look at each element to combine or flatten, so bigger inputs take more time.
Understanding how collection functions scale helps you write efficient Terraform code and explain your reasoning clearly in interviews.
"What if we nested lists three levels deep and used flatten multiple times? How would the time complexity change?"
Practice
length() do when applied to a list?Solution
Step 1: Understand the purpose of length()
Thelength()function counts how many elements are in a list, map, or string.Step 2: Apply to a list context
When used on a list, it returns the total number of items inside that list.Final Answer:
Returns the number of items in the list -> Option AQuick Check:
length(list) = count of items [OK]
- Thinking length() merges lists
- Confusing length() with flatten()
- Assuming length() returns the list itself
map1 and map2 in Terraform?Solution
Step 1: Identify the function for combining maps
Themerge()function is used to combine two or more maps into one.Step 2: Check syntax correctness
The correct syntax ismerge(map1, map2). Other options either use wrong functions or invalid operators.Final Answer:
merge(map1, map2) -> Option DQuick Check:
merge() combines maps correctly [OK]
- Using + operator to merge maps
- Confusing flatten() with merge()
- Passing maps without commas
flatten([["a", "b"], ["c"], ["d", "e"]])
Solution
Step 1: Understand flatten() function
Theflatten()function takes a list of lists and combines all inner lists into one single list.Step 2: Apply flatten() to the given nested list
Input is [["a", "b"], ["c"], ["d", "e"]]. Flattening merges all inner lists into ["a", "b", "c", "d", "e"].Final Answer:
["a", "b", "c", "d", "e"] -> Option BQuick Check:
flatten(nested list) = single list [OK]
- Thinking flatten() returns original nested list
- Expecting flatten() to work on maps
- Confusing flatten() with merge()
locals {
map1 = { a = 1, b = 2 }
map2 = { b = 3, c = 4 }
result = merge(map1, map2)
}What is the value of
local.result?Solution
Step 1: Understand merge() behavior with duplicate keys
When keys overlap, the value from the last map argument overrides earlier ones.Step 2: Apply merge to map1 and map2
map1 has b=2, map2 has b=3. The merged map uses b=3 from map2, so result is { a=1, b=3, c=4 }.Final Answer:
{ a = 1, b = 3, c = 4 } -> Option AQuick Check:
merge() overrides duplicate keys with last map [OK]
- Assuming values add up for duplicate keys
- Expecting error on duplicate keys
- Confusing merge() with flatten()
list_of_maps = [
{ name = "Alice", age = 30 },
{ name = "Bob", age = 25 },
{ name = "Carol", age = 27 }
]Which Terraform expression correctly merges all maps into one map with all keys and values combined?
Solution
Step 1: Understand how to merge multiple maps in a list
To merge all maps inside a list, you use the splat operator...to unpack the list as arguments tomerge().Step 2: Check each option
merge(list_of_maps...) usesmerge(list_of_maps...)which correctly merges all maps. flatten(list_of_maps) tries flatten on maps (invalid), C returns count, D passes list directly (invalid).Final Answer:
merge(list_of_maps...) -> Option CQuick Check:
Use merge() with ... to combine list of maps [OK]
- Passing list directly to merge() without ...
- Using flatten() on maps
- Confusing length() with merge()
Ready to prove it?
Pass the test and this pattern moves unknown
