String functions (join, split, format) in Terraform - Time & Space Complexity
We want to understand how the time to run string functions like join, split, and format changes as the input grows.
How does the work grow when we handle more or bigger strings?
Analyze the time complexity of these Terraform string operations.
locals {
list_of_strings = ["apple", "banana", "cherry"]
joined_string = join(",", local.list_of_strings)
split_list = split(",", local.joined_string)
formatted_str = format("Fruits: %s", local.joined_string)
}
This sequence joins a list into one string, splits it back, and formats a string with the joined result.
Look at what repeats as input size grows.
- Primary operation: Joining strings by concatenating with a separator.
- How many times: Each string in the list is processed once during join and once during split.
As the number of strings increases, the work grows roughly in direct proportion.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 string operations |
| 100 | About 100 string operations |
| 1000 | About 1000 string operations |
Pattern observation: The time grows linearly as the number of strings increases.
Time Complexity: O(n)
This means the time to join, split, or format grows directly with the number of strings.
[X] Wrong: "String join or split takes the same time no matter how many strings there are."
[OK] Correct: Each string must be processed, so more strings mean more work and more time.
Understanding how string operations scale helps you reason about performance in real cloud setups where data size varies.
"What if we used a very long single string instead of many small strings? How would the time complexity change?"
Practice
join(",", ["a", "b", "c"]) return?Solution
Step 1: Understand the join function
Thejoinfunction combines list elements into a single string using the given separator.Step 2: Apply join with separator ','
Joining ["a", "b", "c"] with "," results in the string "a,b,c".Final Answer:
"a,b,c" -> Option AQuick Check:
join with comma = "a,b,c" [OK]
- Confusing join output with original list
- Using space instead of comma as separator
- Expecting a list instead of a string
"one,two,three" into a list using Terraform?Solution
Step 1: Recall split function syntax
The split function takes the separator first, then the string to split.Step 2: Match correct argument order
Onlysplit(",", "one,two,three")matches the correct syntax.Final Answer:
split(",", "one,two,three") -> Option CQuick Check:
split(separator, string) = split(",", "one,two,three") [OK]
- Swapping arguments order in split
- Using join instead of split
- Using format instead of split
format("Hello %s, you have %d messages", "Alice", 5)Solution
Step 1: Understand format placeholders
%sis replaced by string,%dby integer in order.Step 2: Replace placeholders with arguments
"Alice" replaces%s, 5 replaces%d, producing "Hello Alice, you have 5 messages".Final Answer:
"Hello Alice, you have 5 messages" -> Option AQuick Check:
format with %s and %d = formatted string [OK]
- Not replacing placeholders
- Mixing up %s and %d types
- Expecting original string without substitution
locals {
parts = split(",", "apple,banana,orange")
result = join(";", parts)
message = format("Fruits: %s", result)
}Solution
Step 1: Check split function usage
split separator and string are correctly ordered.Step 2: Check join and format usage
join uses string separator ";" correctly; format uses %s for string correctly.Final Answer:
No error, code is correct -> Option DQuick Check:
split, join, format used correctly = No error, code is correct [OK]
- Swapping split arguments
- Using wrong placeholder in format
- Passing wrong types to join
["10.0.0.1", "10.0.0.2", "10.0.0.3"]. You want to create a single string with these IPs separated by commas, but enclosed in square brackets like this:[10.0.0.1,10.0.0.2,10.0.0.3]Which Terraform expression achieves this?
Solution
Step 1: Join IP list with commas
Usingjoin(",", [...])creates "10.0.0.1,10.0.0.2,10.0.0.3".Step 2: Use format to add square brackets
Wrapping the joined string withformat("[%s]", ...)adds brackets around it.Final Answer:
format("[%s]", join(",", ["10.0.0.1", "10.0.0.2", "10.0.0.3"])) -> Option BQuick Check:
join then format = format("[%s]", join(",", ["10.0.0.1", "10.0.0.2", "10.0.0.3"])) [OK]
- Trying to format before join
- Using split instead of join
- Incorrect argument order in format
Ready to prove it?
Pass the test and this pattern moves unknown
