String interpolation in C Sharp (C#) - 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.
string name = "Alice";
int age = 30;
string city = "Wonderland";
string result = $"Name: {name}, Age: {age}, City: {city}";
Console.WriteLine(result);
This code creates a new string by inserting variables into a template using string interpolation.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Combining each variable's value into the final string.
- How many times: Once for each variable included inside the curly braces.
Each added variable means more pieces to join, so the work grows as we add more parts.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 pieces combined |
| 10 | 10 pieces combined |
| 100 | 100 pieces combined |
Pattern observation: The time grows roughly in direct proportion to the number of parts we insert.
Time Complexity: O(n)
This means the time to build the string grows linearly with the number of inserted variables.
[X] Wrong: "String interpolation is instant no matter how many variables are inside."
[OK] Correct: Each variable must be processed and combined, so more variables mean more work and more time.
Understanding how string building scales helps you write efficient code and explain your choices clearly in interviews.
"What if we used a loop to build the string with many variables instead of writing them all inside one interpolation? How would the time complexity change?"
Practice
Solution
Step 1: Understand string interpolation syntax
String interpolation uses a $ before the string and curly braces { } to insert variables inside the string.Step 2: Compare options with this syntax
Only Insert variables directly inside a string using { } with a $ prefix describes inserting variables inside strings using { } with a $ prefix.Final Answer:
Insert variables directly inside a string using { } with a $ prefix -> Option BQuick Check:
String interpolation = $ + {variable} [OK]
- Confusing concatenation (+) with interpolation
- Forgetting the $ before the string
- Using variables outside the string only
Solution
Step 1: Identify correct string interpolation syntax
In C#, string interpolation requires a $ before a double-quoted string and variables inside { }.Step 2: Check each option
string message = $"Hello, {name}!"; uses $ before double quotes and {name} inside, which is correct. The remaining options lack $ or use single quotes which is invalid for interpolation.Final Answer:
string message = $"Hello, {name}!"; -> Option AQuick Check:
$ + "string" + {variable} = correct syntax [OK]
- Using single quotes instead of double quotes
- Omitting the $ symbol
- Placing variables outside { }
int apples = 5;
int oranges = 3;
string result = $"I have {apples + oranges} fruits.";
Console.WriteLine(result);
Solution
Step 1: Evaluate the expression inside the interpolation
The expression {apples + oranges} adds 5 + 3, which equals 8.Step 2: Substitute the result into the string
The string becomes "I have 8 fruits." and is printed.Final Answer:
I have 8 fruits. -> Option AQuick Check:
Expression inside { } is calculated before output [OK]
- Printing the expression text instead of its value
- Concatenating numbers as strings incorrectly
- Ignoring the + operator inside { }
int age = 30;
string message = $"Your age is {age";
Console.WriteLine(message);
Solution
Step 1: Check the interpolation braces
The string has an opening { but no closing } after age, causing a syntax error.Step 2: Verify other parts
The $ is present, age is declared, and Console.WriteLine is correct.Final Answer:
Missing closing brace } in the interpolation -> Option CQuick Check:
Every { must have a matching } [OK]
- Forgetting to close interpolation braces
- Removing $ accidentally
- Assuming variable is undeclared without checking
double price = 12.5;
// Fill in the blank:
string output = $"Price: {___}";
Console.WriteLine(output);
Solution
Step 1: Understand C# string interpolation formatting
Inside { }, you can add :F2 to format a number with 2 decimals.Step 2: Check each option
price:F2 uses {price:F2} which is correct. price:.2 is an invalid format specifier (requires :F2 for fixed decimals). price.ToFixed(2) uses ToFixed which is JavaScript, not C#. "{price:2}" is invalid syntax.Final Answer:
price:F2 -> Option DQuick Check:
Use :F2 inside { } to format decimals [OK]
- Using JavaScript methods like ToFixed in C#
- Trying to format outside the interpolation braces
- Incorrect format string syntax
Ready to prove it?
Pass the test and this pattern moves unknown
