String templates and interpolation in Kotlin - Time & Space Complexity
We want to understand how the time to create strings with templates grows as the input changes.
How does the work increase when we build strings using variables inside them?
Analyze the time complexity of the following code snippet.
fun greet(names: List): List {
val greetings = mutableListOf()
for (name in names) {
greetings.add("Hello, $name!")
}
return greetings
}
This code creates a greeting message for each name in a list using string templates.
- Primary operation: Looping through each name in the list.
- How many times: Once for every name in the input list.
As the number of names grows, the number of greetings created grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 greetings created |
| 100 | 100 greetings created |
| 1000 | 1000 greetings created |
Pattern observation: The work grows directly with the number of names.
Time Complexity: O(n)
This means the time to create greetings grows in a straight line with the number of names.
[X] Wrong: "String templates make the code run slower exponentially because they are complex."
[OK] Correct: Each string template is processed once per item, so the time grows linearly, not exponentially.
Understanding how string building scales helps you write efficient code and explain your reasoning clearly.
"What if we used nested loops to create greetings for pairs of names? How would the time complexity change?"
Practice
name inside a Kotlin string using string templates?Solution
Step 1: Understand Kotlin string templates
Kotlin uses$variableto insert variable values inside strings.Step 2: Identify correct syntax for variable insertion
The syntax$namedirectly inserts the value ofnameinto the string.Final Answer:
println("Hello, $name!") -> Option CQuick Check:
Use $variable for string templates [OK]
- Using curly braces without $ like {name}
- Concatenating with + inside strings
- Using % signs instead of $
Solution
Step 1: Recall expression syntax in string templates
Kotlin uses${expression}to evaluate expressions inside strings.Step 2: Identify correct expression usage
${a + b}correctly calculates the sum and inserts it.Final Answer:
println("Sum is ${a + b}") -> Option AQuick Check:
Use ${expression} for calculations inside strings [OK]
- Using $a + $b prints variables separately, not sum
- Using square brackets [] instead of curly braces
- Using parentheses () without $
val price = 10
val tax = 2
println("Total: ${price + tax}")Solution
Step 1: Evaluate the expression inside the string template
The expression${price + tax}adds 10 + 2 = 12.Step 2: Print the resulting string
The string becomes "Total: 12" and is printed.Final Answer:
Total: 12 -> Option BQuick Check:
Expression inside ${} is evaluated [OK]
- Printing the expression literally without evaluation
- Confusing $variable with ${expression}
- Expecting concatenation without braces
val count = 5
println("Count is $count+1")Solution
Step 1: Understand how $count+1 is interpreted
Kotlin treats$countas variable and+1as plain text, so it prints "5+1" literally.Step 2: Correct way to calculate sum in string
Use${count + 1}to evaluate the sum inside the string.Final Answer:
It will print 'Count is 5+1' literally, not sum -> Option AQuick Check:
Use ${} for expressions, else prints text [OK]
- Assuming $count+1 calculates sum automatically
- Missing braces for expressions
- Confusing string concatenation with templates
$20. Which Kotlin string correctly shows the dollar sign before the number stored in price?Solution
Step 1: Understand escaping dollar sign in Kotlin strings
To print a literal dollar sign, use\$to escape it.Step 2: Combine escaped dollar sign with variable
"\$$price"prints a literal $ followed by the value ofprice.Final Answer:
println("\$$price") -> Option DQuick Check:
Escape $ with \$ to print it literally [OK]
- Not escaping $ prints variable name or causes error
- Using $\$ instead of \$$
- Confusing escape sequences
Ready to prove it?
Pass the test and this pattern moves unknown
