How Strings Work Differently Across Languages in DSA Javascript - Complexity Walkthrough
Strings are used everywhere in programming, but how they behave can change depending on the language.
We want to understand how operations on strings grow in cost as the string size grows.
Analyze the time complexity of concatenating strings in JavaScript.
const concatStrings = (arr) => {
let result = '';
for (let str of arr) {
result += str;
}
return result;
};
This code joins many small strings into one big string by adding each one at the end.
Look at what repeats as the input grows.
- Primary operation: Adding one string to the end of another (string concatenation).
- How many times: Once for each string in the input array.
Each time we add a string, JavaScript creates a new string copying all old characters plus the new ones.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 55 character copies (1+2+...+10) |
| 100 | About 5050 character copies |
| 1000 | About 500,500 character copies |
Pattern observation: The work grows roughly like the square of the number of strings.
Time Complexity: O(n²)
This means if you double the number of strings, the time to join them grows about four times.
[X] Wrong: "Adding strings with += is always fast and linear time."
[OK] Correct: Each += creates a new string copying all previous characters, so the total work adds up quickly.
Understanding how string operations cost time helps you write efficient code and explain your choices clearly in interviews.
"What if we used an array to collect strings and joined them once at the end? How would the time complexity change?"
Practice
Solution
Step 1: Understand string mutability in JavaScript
JavaScript strings cannot be changed after creation; they are immutable.Step 2: Compare with other options
Options A, B, and C suggest mutability or direct modification, which is incorrect.Final Answer:
Strings are immutable, meaning you cannot change a character directly. -> Option AQuick Check:
JavaScript strings are immutable = D [OK]
- Thinking strings behave like arrays and can be changed by index
- Confusing string immutability with array mutability
- Assuming strings are objects with mutable properties
str in JavaScript?Solution
Step 1: Recall JavaScript string length syntax
In JavaScript, string length is accessed as a property without parentheses: str.lengthStep 2: Check other options for syntax errors
str.length() uses parentheses which is incorrect; C and D are not valid JavaScript syntax for string length.Final Answer:
str.length -> Option DQuick Check:
Use property .length without () = A [OK]
- Using parentheses like a function call (str.length())
- Confusing with other languages that use length() function
- Trying to use str.size which is invalid in JavaScript
const s = 'hello'; console.log(s[1]); s[1] = 'a'; console.log(s[1]);
Solution
Step 1: Check first console.log(s[1])
Index 1 of 'hello' is 'e', so first output is 'e'.Step 2: Attempt to change s[1] and check second console.log(s[1])
Strings are immutable, so s[1] = 'a' does nothing; second output remains 'e'.Final Answer:
e\ne -> Option AQuick Check:
Immutable strings keep original chars = B [OK]
- Expecting s[1] to change to 'a'
- Confusing string mutability with arrays
- Assuming assignment to string index works
let str = 'world'; str[0] = 'W'; console.log(str);
Solution
Step 1: Understand string immutability and assignment
Assigning to str[0] does not change the string; no error is thrown.Step 2: Check console.log output
Since assignment fails silently, output remains original string 'world'.Final Answer:
No error; output is 'world' -> Option BQuick Check:
Immutable strings ignore index assignment = C [OK]
- Expecting a TypeError on index assignment
- Thinking string changes after index assignment
- Confusing with languages that allow mutable strings
str with an uppercase letter in JavaScript. Which approach correctly achieves this?Solution
Step 1: Understand string immutability and modification
Direct assignment to str[0] is invalid; strings are immutable.Step 2: Check each option for correct string replacement
str = str[0].toUpperCase() + str.slice(1); creates a new string by uppercasing first char and adding rest; this works correctly.Step 3: Analyze other options
B tries direct assignment (invalid), C uses replace incorrectly (replace needs substring, not index), D uppercases whole string then concatenates first char with rest but doesn't reassign to str (str unchanged).Final Answer:
str = str[0].toUpperCase() + str.slice(1); -> Option CQuick Check:
Use new string with uppercase first char + rest = A [OK]
- Trying to assign directly to str[0]
- Using replace with index instead of substring
- Uppercasing whole string instead of first char only
Ready to prove it?
Pass the test and this pattern moves unknown
