String Basics and Memory Representation in DSA C++ - Time & Space Complexity
We want to understand how the time to work with strings grows as the string gets longer.
How does the number of steps change when we do basic string operations like copying or accessing characters?
Analyze the time complexity of the following code snippet.
#include <iostream>
#include <string>
int main() {
std::string s = "hello";
for (size_t i = 0; i < s.size(); ++i) {
char c = s[i];
std::cout << c;
}
return 0;
}
This code prints each character of a string one by one by accessing each index.
- Primary operation: Accessing each character in the string using indexing.
- How many times: Once for each character in the string (n times, where n is string length).
As the string gets longer, the number of steps grows directly with its length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 character accesses and prints |
| 100 | 100 character accesses and prints |
| 1000 | 1000 character accesses and prints |
Pattern observation: Doubling the string length doubles the work needed.
Time Complexity: O(n)
This means the time to process the string grows in a straight line with its length.
[X] Wrong: "Accessing a character by index in a string is constant time, so the whole loop is constant time."
[OK] Correct: While each character access is fast, the loop runs once for every character, so total time grows with string length.
Understanding how string operations scale helps you write efficient code and explain your reasoning clearly in interviews.
"What if we replaced the loop with a recursive function that processes one character at a time? How would the time complexity change?"
Practice
char str[] = "hello";Solution
Step 1: Understand the declaration type
The code declaresstras a character array initialized with the string "hello".Step 2: Recognize the null terminator in C-style strings
In C++, character arrays initialized with string literals automatically include a null character '\0' at the end to mark the string's end.Final Answer:
An array of characters ending with a null character -> Option AQuick Check:
C-style string = char array + '\0' [OK]
- Confusing array with pointer
- Forgetting the null terminator
- Thinking it's a C++ string object
Solution
Step 1: Check pointer declaration syntax
char *str = "hello";declares a pointer to a string literal correctly.Step 2: Identify errors in other options
char str = "hello"; uses a single char variable for a string literal, which is invalid.
char str[] = 'hello'; uses single quotes for multiple characters, which is invalid.
string str = 'hello'; uses single quotes withstringtype, which requires double quotes.Final Answer:
char *str = "hello"; -> Option AQuick Check:
Pointer to string literal uses char * and double quotes [OK]
- Using single quotes for strings
- Assigning string literal to char variable
- Confusing char array and pointer syntax
char str[] = "abc";
std::cout << sizeof(str) << std::endl;
Solution
Step 1: Understand sizeof on char array initialized with string
The arraystrcontains characters 'a', 'b', 'c' plus a null terminator '\0'. So total 4 characters.Step 2: sizeof returns total bytes of array
Since each char is 1 byte,sizeof(str)returns 4.Final Answer:
4 -> Option DQuick Check:
sizeof(char array with "abc") = 4 [OK]
- Ignoring null terminator in size
- Confusing sizeof with string length
- Expecting runtime output instead of compile-time size
char *str = "hello";
str[0] = 'H';
std::cout << str << std::endl;
Solution
Step 1: Understand string literal immutability
String literals are stored in read-only memory. Modifying them causes undefined behavior.Step 2: Analyze the code's modification attempt
The code tries to change the first character of the string literal via pointer, which is not allowed.Final Answer:
Modifying a string literal causes undefined behavior -> Option CQuick Check:
String literals are immutable in C++ [OK]
- Assuming string literals are modifiable arrays
- Ignoring const correctness
- Confusing pointer initialization with mutability
char str1[] = "abc";
char *str2 = str1;
str2[1] = 'z';
std::cout << str1 << std::endl;
Solution
Step 1: Understand array and pointer relationship
str1is a modifiable character array.str2points to the same array.Step 2: Modify character via pointer
Changingstr2[1]changes the second character ofstr1from 'b' to 'z'.Step 3: Print the modified array
Printingstr1outputs "azc" reflecting the change.Final Answer:
azc -> Option BQuick Check:
Pointer modifies array content directly [OK]
- Thinking string literals are modifiable
- Confusing pointer and array copy
- Expecting original string unchanged
Ready to prove it?
Pass the test and this pattern moves unknown
