0
0
Terraformcloud~5 mins

Numeric functions (min, max, ceil) in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Numeric functions (min, max, ceil)
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

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
10About 10 comparisons for min, 10 for max, and 10 ceil calls
100About 100 comparisons for min, 100 for max, and 100 ceil calls
1000About 1000 comparisons for min, 1000 for max, and 1000 ceil calls

Pattern observation: The work grows evenly and directly with the number of inputs.

Final Time Complexity

Time Complexity: O(n)

This means the time to compute min, max, and ceil grows in a straight line as the list gets longer.

Common Mistake

[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.

Interview Connect

Understanding how simple numeric functions scale helps you reason about performance in real cloud setups where data size changes.

Self-Check

"What if we used nested lists and applied min to each inner list? How would the time complexity change?"