Array Insertion at Beginning in DSA Typescript - Time & Space Complexity
When we add an item at the start of an array, the computer must make room for it.
We want to know how the time needed grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
function insertAtBeginning(arr: number[], value: number): number[] {
for (let i = arr.length; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = value;
return arr;
}
This code shifts all elements one step right to insert a new value at the start.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves each element one position to the right.
- How many times: It runs once for each element in the array, so n times if the array has n elements.
As the array gets bigger, the number of moves grows directly with its size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 moves |
| 100 | 100 moves |
| 1000 | 1000 moves |
Pattern observation: The work grows in a straight line as the array size increases.
Time Complexity: O(n)
This means the time to insert at the beginning grows directly with the number of elements.
[X] Wrong: "Inserting at the start is always fast because it's just one step."
[OK] Correct: Actually, all existing elements must move one step right, so the time depends on the array size.
Understanding this helps you explain why some data structures are better for adding at the front than arrays.
"What if we used a linked list instead of an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand array insertion at beginning
When a new element is inserted at the start, all existing elements move one step to the right to make space.Step 2: Effect on existing elements
This shifting means the first element moves to second position, second to third, and so on.Final Answer:
Shifts all existing elements one position to the right -> Option CQuick Check:
Insert at start shifts elements right [OK]
- Thinking insertion replaces first element
- Assuming array size decreases
- Believing array stays unchanged
Solution
Step 1: Recall array methods for insertion
In TypeScript (JavaScript), `unshift` adds elements at the start of an array.Step 2: Check other options
`push` adds at the end, `insert` and `addFirst` are not valid array methods.Final Answer:
arr.unshift(5); -> Option AQuick Check:
Use unshift() to add at beginning [OK]
- Using push() which adds at the end
- Using non-existent methods like insert()
- Confusing unshift() with pop()
let arr = [10, 20, 30]; arr.unshift(5); console.log(arr);
Solution
Step 1: Understand unshift effect
unshift(5) adds 5 at the start, shifting existing elements right.Step 2: Resulting array
Original [10, 20, 30] becomes [5, 10, 20, 30].Final Answer:
[5, 10, 20, 30] -> Option BQuick Check:
unshift adds at front [OK]
- Thinking unshift adds at end
- Misplacing the new element in the array
- Confusing unshift with push
let nums = [1, 2, 3]; nums.add(0, 100); console.log(nums);
Solution
Step 1: Check array method usage
Arrays in TypeScript do not have an `add` method; insertion at beginning uses `unshift`.Step 2: Validate other parts
Declaration and console.log are correct; adding 100 is allowed.Final Answer:
`add` is not a valid method for arrays -> Option AQuick Check:
Use unshift(), not add() [OK]
- Using non-existent add() method
- Thinking console.log is wrong
- Believing arrays can't hold numbers
Solution
Step 1: Understand insertion at beginning
Each unshift adds element at front, pushing existing elements right.Step 2: Trace operations
Starting empty: unshift(3) -> [3], unshift(2) -> [2,3], unshift(1) -> [1,2,3].Final Answer:
data.unshift(3); data.unshift(2); data.unshift(1); -> Option DQuick Check:
Insert in reverse order with unshift [OK]
- Using push() which adds at end
- Inserting elements in wrong order
- Mixing push and unshift incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
