Character arrays in C++ - Time & Space Complexity
When working with character arrays, it is important to understand how the time to process them grows as the array gets bigger.
We want to know how the number of steps changes when we read or manipulate these arrays.
Analyze the time complexity of the following code snippet.
char arr[] = "hello world";
int length = 0;
while (arr[length] != '\0') {
length++;
}
This code counts how many characters are in the array until it finds the end marker.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each character in the array one by one.
- How many times: Once for each character until the end marker is found.
As the array gets longer, the number of checks grows directly with its size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows in a straight line with the size of the array.
Time Complexity: O(n)
This means the time to count characters grows directly with the number of characters.
[X] Wrong: "The loop runs a fixed number of times regardless of array size."
[OK] Correct: The loop depends on the array length, so it runs more times as the array gets longer.
Understanding how loops over character arrays scale helps you explain simple string operations clearly in interviews.
"What if we used a function that copies the array instead of just counting? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of '\0' in character arrays
The null terminator '\0' is a special character used to mark the end of a string in C++ character arrays.Step 2: Identify its effect on string handling
Functions that work with strings stop reading characters when they encounter '\0', so it tells where the string ends.Final Answer:
It marks the end of the string stored in the array. -> Option AQuick Check:
Null terminator = end of string [OK]
- Thinking '\0' initializes characters to zero
- Confusing '\0' with increasing array size
- Assuming '\0' changes character case
Solution
Step 1: Count characters and null terminator
The word "code" has 4 letters plus 1 null terminator '\0', so the array size must be 5.Step 2: Check each option's array size and initialization
char word[5] = {'c', 'o', 'd', 'e', '\0'}; correctly declares a char array of size 5 and includes the null terminator explicitly.Final Answer:
char word[5] = {'c', 'o', 'd', 'e', '\0'}; -> Option DQuick Check:
Array size = letters + 1 for '\0' [OK]
- Forgetting to add space for the null terminator
- Using incorrect array size
- Not including '\0' explicitly when needed
char arr[6] = "hello"; arr[0] = 'H'; std::cout << arr << std::endl;
Solution
Step 1: Understand the initial array content
The array is initialized with "hello" which includes the null terminator automatically, so arr = {'h','e','l','l','o','\0'}.Step 2: Modify the first character and print
Changing arr[0] to 'H' updates the first letter. Printing arr outputs the string until '\0', so output is "Hello".Final Answer:
Hello -> Option BQuick Check:
Changing arr[0] changes first letter [OK]
- Expecting output to be lowercase 'hello'
- Thinking only one character prints
- Assuming compilation error due to modification
char name[4] = "John"; std::cout << name << std::endl;
Solution
Step 1: Check array size vs string length
The string "John" has 4 characters plus a null terminator, so it needs an array of size 5.Step 2: Understand consequences of small array
Declaring name[4] leaves no space for '\0', causing undefined behavior when printing.Final Answer:
Array size is too small to hold the string and null terminator. -> Option CQuick Check:
Array size must include space for '\0' [OK]
- Ignoring null terminator space
- Thinking printing char arrays is invalid
- Confusing syntax errors with size errors
Solution
Step 1: Declare array with correct size for "Hi!"
The phrase "Hi!" has 3 characters plus 1 for '\0', so array size must be 4.Step 2: Replace '!' with '?' at correct index
The '!' is at index 2, so phrase[2] = '?' correctly replaces it.Step 3: Print the modified phrase
Printing phrase outputs "Hi?" as expected.Final Answer:
char phrase[4] = "Hi!"; phrase[2] = '?'; std::cout << phrase << std::endl; -> Option AQuick Check:
Array size = 4; replace index 2 [OK]
- Using array size too small to hold string and '\0'
- Replacing wrong index character
- Confusing initialization with replacement
Ready to prove it?
Pass the test and this pattern moves unknown
