String interpolation in Swift - Time & Space Complexity
We want to understand how the time it takes to create a string using interpolation changes as the input grows.
How does the work increase when we add more parts to the string?
Analyze the time complexity of the following code snippet.
let name = "Alice"
let age = 30
let greeting = "Hello, \(name)! You are \(age) years old."
print(greeting)
This code creates a greeting message by inserting variables into a string using interpolation.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying characters and inserting variable values into the string.
- How many times: Once for each character and variable inserted.
As the length of the variables or the number of interpolations grows, the work to build the string grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to copy and insert |
| 100 | About 100 steps to copy and insert |
| 1000 | About 1000 steps to copy and insert |
Pattern observation: The work grows roughly in direct proportion to the total length of the final string.
Time Complexity: O(n)
This means the time to create the string grows linearly with the size of the string being built.
[X] Wrong: "String interpolation is instant and does not depend on input size."
[OK] Correct: Actually, the program must copy and combine all parts, so bigger strings take more time.
Understanding how string building grows with input size helps you explain performance in real apps, showing you know how simple operations add up.
"What if we used a loop to build a string by adding many interpolated parts? How would the time complexity change?"
Practice
Solution
Step 1: Understand string interpolation purpose
String interpolation lets you put variables or expressions inside a string easily.Step 2: Recognize the syntax
Using \( ) inside double quotes inserts the value dynamically.Final Answer:
Insert variables or expressions directly inside a string using \( ) -> Option DQuick Check:
String interpolation = Insert variables with \( ) [OK]
- Confusing interpolation with string repetition
- Trying to use single quotes instead of double quotes
- Forgetting the backslash before parentheses
score inside a string?Solution
Step 1: Identify correct interpolation syntax
Swift uses \(variable) inside double quotes for interpolation.Step 2: Check each option
"Your score is: \(score)" uses \(score), which is correct. Others use wrong syntax.Final Answer:
"Your score is: \(score)" -> Option CQuick Check:
Swift interpolation = \(variable) [OK]
- Using ${} like in other languages
- Forgetting the backslash before parentheses
- Not using double quotes around the string
let name = "Anna"
let age = 30
print("\(name) is \(age) years old.")Solution
Step 1: Understand variable values
name = "Anna", age = 30.Step 2: Apply string interpolation
"\(name)" becomes "Anna", "\(age)" becomes "30" in the string.Final Answer:
Anna is 30 years old. -> Option AQuick Check:
Interpolation replaces variables with values [OK]
- Printing the literal \(name) instead of value
- Confusing variable names with strings
- Missing backslash causing no interpolation
let temperature = 22
print("The temperature is (temperature) degrees.")Solution
Step 1: Check interpolation syntax
Interpolation requires a backslash before parentheses: \(variable).Step 2: Identify missing backslash
Code uses (temperature) without backslash, so it won't interpolate.Final Answer:
Missing backslash before parentheses in interpolation -> Option AQuick Check:
Backslash is required for interpolation [OK]
- Forgetting the backslash
- Using wrong quote types
- Assuming parentheses alone interpolate
price = 50 and taxRate = 0.1, which Swift code correctly uses string interpolation to print "Total price: 55.0"?Solution
Step 1: Calculate total price with tax
Total price = price * (1 + taxRate) = 50 * 1.1 = 55.0.Step 2: Check each option's expression
print("Total price: \(price * (1 + taxRate))"): print("Total price: \(price * (1 + taxRate))") = 55.0 correct. A: \(price + taxRate) = 50.1 wrong. C: \(price * taxRate) = 5.0 (tax only) wrong. D: \(price + taxRate * 100) = 60 wrong.Final Answer:
print("Total price: \(price * (1 + taxRate))") -> Option BQuick Check:
Use price * (1 + taxRate) for total [OK]
- Adding taxRate directly instead of multiplying
- Forgetting parentheses around (1 + taxRate)
- Multiplying taxRate by 100 incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
