String Basics and Memory Representation in DSA Javascript - 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?
Analyze the time complexity of the following code snippet.
const str = "hello world";
let result = "";
for (let i = 0; i < str.length; i++) {
result += str[i];
}
console.log(result);
This code copies each character from one string to another by looping through the original string.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the string.
- How many times: Once for each character in the string (n times).
As the string gets longer, the loop runs more times, so the work grows with the string length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character copies |
| 100 | About 100 character copies |
| 1000 | About 1000 character copies |
Pattern observation: The number of operations grows directly with the string length.
Time Complexity: O(n^2)
This means the time to copy the string grows quadratically with the string length due to repeated string concatenation.
[X] Wrong: "Copying a string is instant and does not depend on its length."
[OK] Correct: Each character must be handled one by one, so longer strings take more time.
Understanding how string operations scale helps you write efficient code and explain your choices clearly in interviews.
"What if we used string concatenation inside the loop versus using an array and joining at the end? How would the time complexity change?"
Practice
immutable mean when talking about strings in JavaScript?Solution
Step 1: Understand string immutability
In JavaScript, once a string is created, its content cannot be changed directly.Step 2: Compare options with definition
Strings cannot be changed after they are created - this matches the definition.Final Answer:
Strings cannot be changed after they are created -> Option AQuick Check:
Immutable means unchangeable = D [OK]
- Thinking strings can be changed like arrays
- Confusing immutability with emptiness
- Assuming strings are stored as numbers
Solution
Step 1: Check string syntax rules
Strings must be enclosed in quotes: single (' '), double (" "), or backticks (` `).Step 2: Validate each option
const str = 'Hello World'; uses single quotes correctly. const str = Hello World; misses quotes. const str = `Hello World; misses closing backtick. const str = (Hello World); uses parentheses incorrectly.Final Answer:
const str = 'Hello World'; -> Option DQuick Check:
Quotes needed for strings = B [OK]
- Forgetting quotes around string text
- Using mismatched or missing quotes
- Using parentheses instead of quotes
let s = 'hello'; s[0] = 'H'; console.log(s);
Solution
Step 1: Understand string immutability in code
Strings cannot be changed by assigning to an index like s[0] = 'H'. This operation is ignored.Step 2: Check what console.log prints
The original string 'hello' remains unchanged and is printed.Final Answer:
hello -> Option AQuick Check:
Strings unchanged by index assignment = A [OK]
- Expecting first letter to change
- Thinking strings behave like arrays
- Assuming error will be thrown
let word = 'code'; word[2] = 'd'; console.log(word);
Solution
Step 1: Analyze string modification attempt
Assigning to a string index like word[2] = 'd' does not change the string because strings are immutable.Step 2: Check other options for errors
Semicolons are optional in JavaScript, variable is declared, and console.log syntax is correct.Final Answer:
Strings are immutable; cannot assign to index -> Option BQuick Check:
Immutable strings block index assignment = C [OK]
- Thinking semicolon is mandatory here
- Assuming variable is undeclared
- Believing console.log syntax is incorrect
const str1 = 'abc'; const str2 = str1 + 'd'; console.log(str1); console.log(str2);
Choose the correct output representation.
Solution
Step 1: Understand string concatenation and immutability
str1 remains 'abc' because strings are immutable. str2 is a new string 'abcd' created by adding 'd' to str1.Step 2: Check console.log outputs
console.log(str1) prints abc. console.log(str2) prints abcd.Final Answer:
abc and abcd printed on separate lines -> Option CQuick Check:
Original unchanged, new string created = A [OK]
- Thinking original string changes after concatenation
- Expecting error due to immutability
- Confusing order of printed strings
Ready to prove it?
Pass the test and this pattern moves unknown
