Array Insertion at Beginning in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to add an item at the start of an array.
How does the time needed change as the array grows bigger?
Analyze the time complexity of the following code snippet.
const arr = [1, 2, 3, 4, 5];
arr.unshift(0); // Insert 0 at the beginning
console.log(arr);
This code adds a new element at the start of the array, shifting all other elements right.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Shifting all existing elements one position to the right.
- How many times: Once for each element in the array.
As the array gets bigger, the number of elements to move grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 moves |
| 100 | About 100 moves |
| 1000 | About 1000 moves |
Pattern observation: The work grows directly with the number of elements.
Time Complexity: O(n)
This means the time to insert at the start grows linearly with the array size.
[X] Wrong: "Adding at the beginning is as fast as adding at the end."
[OK] Correct: Adding at the start requires moving all elements, while adding at the end just adds one item.
Knowing how array operations scale helps you choose the right data structure and explain your choices clearly.
"What if we used a linked list instead of an array? How would the time complexity change?"
Practice
unshift() method do in a JavaScript array?Solution
Step 1: Understand the
Theunshift()methodunshift()method adds elements to the start of an array, shifting existing elements to the right.Step 2: Compare with other array methods
push()adds to the end,shift()removes from the start, andpop()removes from the end.Final Answer:
Adds one or more elements to the beginning of the array -> Option AQuick Check:
unshift()adds at start = B [OK]
- Confusing unshift() with push()
- Thinking unshift() removes elements
- Mixing unshift() with shift()
10 at the beginning of array arr?Solution
Step 1: Recall JavaScript array methods
JavaScript arrays useunshift()to add elements at the start.Step 2: Check each option
Onlyarr.unshift(10);is valid JavaScript syntax for this operation.Final Answer:
arr.unshift(10); -> Option CQuick Check:
Correct syntax for start insertion = A [OK]
- Using non-existent methods like addFirst or pushFront
- Confusing unshift() with push()
- Trying to use insertAt which is not a JS array method
let arr = [2, 3, 4]; arr.unshift(1); console.log(arr);
Solution
Step 1: Understand unshift effect on array
Theunshift(1)adds 1 at the start, shifting existing elements right.Step 2: Trace the array after unshift
Original array: [2, 3, 4]
After unshift(1): [1, 2, 3, 4]Final Answer:
[1, 2, 3, 4] -> Option BQuick Check:
unshift adds at front = [1, 2, 3, 4] [OK]
- Thinking unshift adds at end
- Ignoring that unshift shifts elements
- Expecting original array unchanged
0 at the beginning of array nums:let nums = [1, 2, 3]; nums.unshift = 0; console.log(nums);
Solution
Step 1: Analyze the assignment to unshift property
The code assigns 0 tonums.unshift, overwriting the method instead of calling it.Step 2: Correct usage of unshift
To add 0 at start, callnums.unshift(0);as a function, not assign a value.Final Answer:
Using assignment instead of calling unshift as a function -> Option AQuick Check:
unshift must be called, not assigned = A [OK]
- Assigning value to unshift instead of calling it
- Missing parentheses when calling unshift
- Thinking unshift is a property, not a method
let data = [5, 6, 7];, which code snippet correctly inserts the numbers 2, 3, 4 at the beginning so that the final array is [2, 3, 4, 5, 6, 7]?Solution
Step 1: Understand unshift with multiple arguments
Theunshift()method can take multiple arguments and adds them all at the start in order.Step 2: Analyze each option's effect
data.unshift([2, 3, 4]); adds a single array as first element, resulting in nested array.
data.unshift(2, 3, 4); adds 2, 3, 4 individually at start, producing desired array.
data.push(2, 3, 4); adds elements at end.
data.splice(0, 0, [2, 3, 4]); inserts a single array at index 0, nesting again.Final Answer:
data.unshift(2, 3, 4); -> Option DQuick Check:
unshift multiple args adds all at front = D [OK]
- Passing an array instead of separate values to unshift
- Using push() which adds at end
- Using splice() incorrectly causing nested arrays
Ready to prove it?
Pass the test and this pattern moves unknown
