String Basics and Memory Representation in DSA C# - Time & Space Complexity
We want to understand how the time to work with strings changes as the string gets longer.
How does the length of a string affect the time to do basic operations like copying or comparing?
Analyze the time complexity of the following code snippet.
string CopyString(string input)
{
string copy = "";
for (int i = 0; i < input.Length; i++)
{
copy += input[i];
}
return copy;
}
This code creates a copy of the input string by adding each character one by one.
- Primary operation: Looping through each character of the input string.
- How many times: Exactly once for each character, so as many times as the string length.
As the string gets longer, the number of steps grows more than just the length because adding to a string inside a loop creates new strings each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 55 operations |
| 100 | About 5,050 operations |
| 1000 | About 500,500 operations |
Pattern observation: The operations grow roughly with the square of the string length because each addition copies the whole string so far.
Time Complexity: O(n²)
This means the time to copy the string grows very quickly as the string gets longer, because each step copies more characters.
[X] Wrong: "Adding characters one by one to a string inside a loop is fast and takes linear time."
[OK] Correct: Each addition creates a new string by copying all characters so far, making the total work grow much faster than the string length.
Understanding how string operations cost time helps you write efficient code and explain your choices clearly in interviews.
"What if we used a StringBuilder instead of adding characters directly? How would the time complexity change?"
Practice
string type in C# represent in memory?Solution
Step 1: Understand string storage in C#
C# strings store characters as Unicode, allowing international text.Step 2: Compare options with string behavior
Only A sequence of characters stored as Unicode values correctly describes strings as sequences of Unicode characters.Final Answer:
A sequence of characters stored as Unicode values -> Option BQuick Check:
string = Unicode characters [OK]
- Thinking strings are mutable arrays
- Confusing ASCII with Unicode
- Assuming strings are just pointers
Solution
Step 1: Check string literal syntax
In C#, string literals use double quotes, so "hello" is correct.Step 2: Validate each option
string s = 'hello'; uses single quotes (for chars), B misses quotes, D tries to assign string to char array directly (invalid).Final Answer:
string s = "hello"; -> Option AQuick Check:
Strings use double quotes in C# [OK]
- Using single quotes for strings
- Forgetting quotes around text
- Assigning string directly to char array
string s1 = "hello";
string s2 = "hello";
Console.WriteLine(object.ReferenceEquals(s1, s2));
Solution
Step 1: Understand string interning in C#
String literals with same content are interned and share the same reference.Step 2: Check ReferenceEquals for s1 and s2
Since both are "hello" literals, ReferenceEquals returns true.Final Answer:
true -> Option AQuick Check:
String literals share references = true [OK]
- Assuming ReferenceEquals returns false for equal strings
- Confusing value equality with reference equality
- Expecting runtime errors
string s = "test";
s[0] = 'T';
Console.WriteLine(s);
Solution
Step 1: Check string mutability in C#
Strings are immutable; individual characters cannot be changed by index.Step 2: Analyze assignment to s[0]
Assigning to s[0] causes a compile-time error because strings cannot be modified this way.Final Answer:
Strings are immutable; cannot assign to s[0] -> Option CQuick Check:
Strings immutable = assignment error [OK]
- Trying to modify string characters directly
- Expecting runtime error instead of compile error
- Assuming strings behave like char arrays
string s = "hello";
string t = s + " world";
Console.WriteLine(s);
Console.WriteLine(t);
Choose the correct output representation.
Solution
Step 1: Understand string concatenation and immutability
Concatenation creates a new string; original string s remains unchanged.Step 2: Trace the output statements
Console.WriteLine(s) prints hello; Console.WriteLine(t) prints hello world.Final Answer:
hello hello world -> Option DQuick Check:
Original string unchanged, new string created [OK]
- Assuming original string changes after concatenation
- Mixing order of printed strings
- Confusing string references with values
Ready to prove it?
Pass the test and this pattern moves unknown
