Bird
Raised Fist0
Terraformcloud~20 mins

Collection functions (length, flatten, merge) in Terraform - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Terraform Collection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of length function on a list?
Given the Terraform expression length(["apple", "banana", "cherry"]), what is the output?
Terraform
length(["apple", "banana", "cherry"])
A2
B3
C0
DError
Attempts:
2 left
💡 Hint
Count the number of items inside the list.
❓ query_result
intermediate
2:00remaining
What does flatten do to nested lists?
What is the result of flatten([[1, 2], [3, 4], [5]]) in Terraform?
Terraform
flatten([[1, 2], [3, 4], [5]])
AError
B[[1, 2], [3, 4], [5]]
C[1, 2, 3, 4, 5]
D[1, [2, 3], 4, 5]
Attempts:
2 left
💡 Hint
Flatten removes one level of nested lists.
📝 Syntax
advanced
2:00remaining
Which merge expression correctly combines two maps?
Select the Terraform expression that correctly merges two maps {a = 1, b = 2} and {b = 3, c = 4} with the second map overriding keys from the first.
Amerge({a = 1, b = 2} + {b = 3, c = 4})
Bmerge({a = 1, b = 2}, {b: 3, c: 4})
Cmerge([{a = 1, b = 2}, {b = 3, c = 4}])
Dmerge({a = 1, b = 2}, {b = 3, c = 4})
Attempts:
2 left
💡 Hint
Use merge with two separate map arguments.
❓ optimization
advanced
2:00remaining
Optimizing nested flatten calls
Given a nested list [[[1], [2, 3]], [[4]]], which expression efficiently flattens it into a single list [1, 2, 3, 4]?
Aflatten(flatten([[[1], [2, 3]], [[4]]]))
Bflatten([1, 2, 3, 4])
Cflatten(flatten(flatten([[[1], [2, 3]], [[4]]])))
Dflatten([[[1], [2, 3]], [[4]]])
Attempts:
2 left
💡 Hint
Use flatten twice to remove two levels of nesting.
🧠 Conceptual
expert
2:00remaining
What is the length of a merged map?
If you merge two maps {x = 10, y = 20} and {y = 30, z = 40}, what is the length of the resulting map?
Terraform
length(merge({x = 10, y = 20}, {y = 30, z = 40}))
A3
B4
C2
DError
Attempts:
2 left
💡 Hint
Keys that appear in both maps are overridden, not duplicated.

Practice

(1/5)
1. What does the Terraform function length() do when applied to a list?
easy
A. Returns the number of items in the list
B. Combines all items into a single string
C. Flattens nested lists into one list
D. Merges the list with another list

Solution

  1. Step 1: Understand the purpose of length()

    The length() function counts how many elements are in a list, map, or string.
  2. Step 2: Apply to a list context

    When used on a list, it returns the total number of items inside that list.
  3. Final Answer:

    Returns the number of items in the list -> Option A
  4. Quick Check:

    length(list) = count of items [OK]
Hint: Count items with length() to get list size fast [OK]
Common Mistakes:
  • Thinking length() merges lists
  • Confusing length() with flatten()
  • Assuming length() returns the list itself
2. Which of the following is the correct syntax to merge two maps map1 and map2 in Terraform?
easy
A. length(map1, map2)
B. flatten(map1, map2)
C. map1 + map2
D. merge(map1, map2)

Solution

  1. Step 1: Identify the function for combining maps

    The merge() function is used to combine two or more maps into one.
  2. Step 2: Check syntax correctness

    The correct syntax is merge(map1, map2). Other options either use wrong functions or invalid operators.
  3. Final Answer:

    merge(map1, map2) -> Option D
  4. Quick Check:

    merge() combines maps correctly [OK]
Hint: Use merge() with parentheses and commas for maps [OK]
Common Mistakes:
  • Using + operator to merge maps
  • Confusing flatten() with merge()
  • Passing maps without commas
3. What is the output of this Terraform expression?
flatten([["a", "b"], ["c"], ["d", "e"]])
medium
A. [["a", "b"], ["c"], ["d", "e"]]
B. ["a", "b", "c", "d", "e"]
C. ["a", "b", ["c"], "d", "e"]
D. Error: flatten() only works on maps

Solution

  1. Step 1: Understand flatten() function

    The flatten() function takes a list of lists and combines all inner lists into one single list.
  2. 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"].
  3. Final Answer:

    ["a", "b", "c", "d", "e"] -> Option B
  4. Quick Check:

    flatten(nested list) = single list [OK]
Hint: flatten() removes inner list brackets to one list [OK]
Common Mistakes:
  • Thinking flatten() returns original nested list
  • Expecting flatten() to work on maps
  • Confusing flatten() with merge()
4. Given the code snippet:
locals {
  map1 = { a = 1, b = 2 }
  map2 = { b = 3, c = 4 }
  result = merge(map1, map2)
}

What is the value of local.result?
medium
A. { a = 1, b = 3, c = 4 }
B. { a = 1, b = 2, c = 4 }
C. { a = 1, b = 5, c = 4 }
D. Error: duplicate keys not allowed in merge

Solution

  1. Step 1: Understand merge() behavior with duplicate keys

    When keys overlap, the value from the last map argument overrides earlier ones.
  2. 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 }.
  3. Final Answer:

    { a = 1, b = 3, c = 4 } -> Option A
  4. Quick Check:

    merge() overrides duplicate keys with last map [OK]
Hint: Last map's keys override earlier ones in merge() [OK]
Common Mistakes:
  • Assuming values add up for duplicate keys
  • Expecting error on duplicate keys
  • Confusing merge() with flatten()
5. You have a list of maps:
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?
hard
A. length(list_of_maps)
B. flatten(list_of_maps)
C. merge(list_of_maps...)
D. merge(list_of_maps)

Solution

  1. 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 to merge().
  2. Step 2: Check each option

    merge(list_of_maps...) uses merge(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).
  3. Final Answer:

    merge(list_of_maps...) -> Option C
  4. Quick Check:

    Use merge() with ... to combine list of maps [OK]
Hint: Use ... to unpack list when merging maps [OK]
Common Mistakes:
  • Passing list directly to merge() without ...
  • Using flatten() on maps
  • Confusing length() with merge()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard