Numeric functions (min, max, ceil) in Terraform - Time & Space Complexity
We want to understand how the time to compute numeric functions like min, max, and ceil changes as we increase the number of inputs.
Specifically, how does the work grow when we use these functions on larger lists of numbers?
Analyze the time complexity of this Terraform code snippet:
variable "numbers" {
type = list(number)
}
output "min_value" {
value = min(var.numbers)
}
output "max_value" {
value = max(var.numbers)
}
output "ceil_values" {
value = [for n in var.numbers : ceil(n)]
}
This code finds the smallest and largest numbers from a list, and rounds each number up to the nearest whole number.
Look at what repeats when the list size grows:
- Primary operation: Comparing numbers for min and max, and applying ceil to each number.
- How many times: Each number is checked once for min and max, and ceil is applied once per number.
As the list gets bigger, the number of comparisons and ceil calculations grows directly with the list size.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 comparisons for min, 10 for max, and 10 ceil calls |
| 100 | About 100 comparisons for min, 100 for max, and 100 ceil calls |
| 1000 | About 1000 comparisons for min, 1000 for max, and 1000 ceil calls |
Pattern observation: The work grows evenly and directly with the number of inputs.
Time Complexity: O(n)
This means the time to compute min, max, and ceil grows in a straight line as the list gets longer.
[X] Wrong: "Using min or max on a list takes the same time no matter how big the list is."
[OK] Correct: Actually, these functions must look at each number once, so more numbers mean more work.
Understanding how simple numeric functions scale helps you reason about performance in real cloud setups where data size changes.
"What if we used nested lists and applied min to each inner list? How would the time complexity change?"
Practice
min(3, 7, 2) return?Solution
Step 1: Understand the
Theminfunctionminfunction returns the smallest number from the given list.Step 2: Compare the numbers 3, 7, and 2
Among 3, 7, and 2, the smallest number is 2.Final Answer:
2 -> Option DQuick Check:
min(3,7,2) = 2 [OK]
- Confusing min with max
- Expecting average instead of minimum
- Using ceil instead of min
ceil function in Terraform?Solution
Step 1: Recall Terraform function syntax
Terraform functions use parentheses to enclose arguments, likefunction_name(argument).Step 2: Check each option
Onlyceil(4.3)uses correct function call syntax.Final Answer:
ceil(4.3) -> Option AQuick Check:
Function calls use parentheses [OK]
- Using spaces instead of parentheses
- Using brackets or braces instead of parentheses
- Missing parentheses
max(5, ceil(3.2), min(4, 2))Solution
Step 1: Evaluate inner functions
ceil(3.2)rounds 3.2 up to 4.min(4, 2)returns 2.Step 2: Evaluate max function
Now the expression ismax(5, 4, 2). The largest number is 5.Final Answer:
5 -> Option BQuick Check:
max(5,4,2) = 5 [OK]
- Ignoring nested function calls
- Mixing up min and max
- Forgetting ceil rounds up
ceil(5.7, 2)Solution
Step 1: Check the ceil function signature
Theceilfunction takes exactly one argument, a decimal number.Step 2: Analyze the given expression
The expressionceil(5.7, 2)has two arguments, which is invalid.Final Answer:
Too many arguments for ceil function -> Option CQuick Check:
ceil expects one argument [OK]
- Passing multiple arguments
- Confusing ceil with round
- Using wrong argument types
Solution
Step 1: Find the minimum of the numbers
min(3.4, 2.1, 5.9)returns 2.1, the smallest number.Step 2: Apply ceil to round up to the nearest whole number
ceil(2.1)rounds 2.1 up to 3.Final Answer:
ceil(min(3.4, 2.1, 5.9)) -> Option AQuick Check:
min=2.1, ceil(2.1)=3 [OK]
- Applying ceil before min
- Using max instead of min
- Confusing order of functions
Ready to prove it?
Pass the test and this pattern moves unknown
