String Basics and Memory Representation in DSA Typescript - Time & Space Complexity
We want to understand how the time to work with strings grows as the string gets longer.
How does the length of a string affect the time needed to read or copy it?
Analyze the time complexity of the following code snippet.
function copyString(input: string): string {
let result = [];
for (let i = 0; i < input.length; i++) {
result.push(input[i]);
}
return result.join('');
}
This code copies a string character by character into a new string.
- 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 roughly the same as the string length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The time grows directly with the string length.
Time Complexity: O(n)
This means the time to copy the string grows in direct proportion to its length.
[X] Wrong: "Copying a string is instant and does not depend on length."
[OK] Correct: Each character must be handled, so longer strings take more time.
Understanding how string operations scale helps you write efficient code and answer questions about performance clearly.
"What if we used built-in string methods that copy the whole string at once? How would the time complexity change?"
Practice
immutable mean when talking about strings in TypeScript?Solution
Step 1: Understand string immutability
Strings in TypeScript cannot be changed after creation. Any modification creates a new string.Step 2: Compare options with definition
Only Once created, the string cannot be changed. correctly states that strings cannot be changed once created.Final Answer:
Once created, the string cannot be changed. -> Option AQuick Check:
Immutable means unchangeable = A [OK]
- Thinking strings can be changed like arrays
- Confusing mutable and immutable
- Assuming strings are stored as numbers
Solution
Step 1: Check variable declaration syntax
TypeScript useslet variableName: type = value;syntax.Step 2: Match type and value
let name: string = 'Alice'; declares a string variable with a string value correctly.Final Answer:
let name: string = 'Alice'; -> Option CQuick Check:
Correct string declaration = A [OK]
- Assigning number to string type
- Missing type annotation
- Using wrong syntax for declaration
let greeting = 'Hello'; greeting[0] = 'Y'; console.log(greeting);
Solution
Step 1: Understand string immutability in TypeScript
Strings cannot be changed by index assignment; such operations do not modify the string.Step 2: Analyze the code behavior
Assigning greeting[0] = 'Y' does nothing; greeting remains 'Hello'.Final Answer:
Hello -> Option DQuick Check:
Strings immutable, no change = Hello [OK]
- Expecting first letter to change
- Thinking string is mutable like array
- Assuming error will be thrown
let str: string = 'Test'; str[1] = 'a'; console.log(str);
Solution
Step 1: Check string mutability rules
Strings are immutable; assigning to an index does nothing.Step 2: Analyze the code behavior
str[1] = 'a' does nothing; str remains 'Test', console.log outputs 'Test'. No error.Final Answer:
No error, output is 'Test' -> Option AQuick Check:
Strings immutable, index assignment ignored = 'Test' [OK]
- Assuming no error and string changes
- Confusing syntax error with runtime error
- Expecting ReferenceError
let s1 = 'abc'; let s2 = s1 + 'd'; console.log(s1); console.log(s2);
Choose the correct output considering string immutability and memory representation.
Solution
Step 1: Understand string concatenation and immutability
Concatenating strings creates a new string; original strings remain unchanged.Step 2: Trace variable values after operations
s1 remains 'abc'; s2 becomes 'abcd'.Final Answer:
abc
abcd -> Option BQuick Check:
Original unchanged, new string created = abc and abcd [OK]
- Expecting s1 to change after concatenation
- Thinking concatenation modifies original string
- Assuming runtime error on concatenation
Ready to prove it?
Pass the test and this pattern moves unknown
