Array Insertion at End in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to add an item at the end of an array.
This helps us know how fast this operation is as the array grows.
Analyze the time complexity of the following code snippet.
const arr = [];
function insertAtEnd(value) {
arr[arr.length] = value;
}
insertAtEnd(5);
insertAtEnd(10);
insertAtEnd(15);
This code adds new values to the end of an array one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding one item at the end of the array.
- How many times: Each call adds exactly one item, no loops inside.
Adding one item takes about the same time no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 insertions, each quick |
| 100 | 100 insertions, each quick |
| 1000 | 1000 insertions, each quick |
Pattern observation: Time per insertion stays about the same as array grows.
Time Complexity: O(1)
This means adding an item at the end takes a constant amount of time, no matter the array size.
[X] Wrong: "Adding at the end takes longer as the array grows because it has to move all items."
[OK] Correct: Arrays keep track of their length, so adding at the end just places the item there without moving others.
Knowing that adding at the end is fast helps you choose the right data structure and explain your code clearly in interviews.
"What if we had to insert at the beginning of the array instead? How would the time complexity change?"
Practice
push() method do when used on a JavaScript array?Solution
Step 1: Understand the purpose of
Thepush()push()method is designed to add elements to the end of an array.Step 2: Compare with other array methods
Other methods likeshift()remove elements,sort()orders elements, andreverse()changes order, butpush()only adds at the end.Final Answer:
Adds one or more elements to the end of the array -> Option AQuick Check:
push()adds elements at end [OK]
- Confusing push() with pop() which removes elements
- Thinking push() adds elements at the start
- Mixing push() with sort() or reverse()
5 at the end of array arr?Solution
Step 1: Recall JavaScript array methods
JavaScript arrays usepush()to add elements at the end.Step 2: Check each option's validity
add(),insertEnd(), andappend()are not valid JavaScript array methods.Final Answer:
arr.push(5); -> Option DQuick Check:
Correct syntax for adding at end is push() [OK]
- Using non-existent methods like add() or append()
- Confusing push() with unshift() which adds at start
- Missing parentheses in method call
let arr = [1, 2, 3]; arr.push(4); console.log(arr);
Solution
Step 1: Understand initial array and push operation
The array starts as [1, 2, 3]. Thepush(4)adds 4 at the end.Step 2: Determine final array content
After push, the array becomes [1, 2, 3, 4].Final Answer:
[1, 2, 3, 4] -> Option CQuick Check:
push(4) adds 4 at end [OK]
- Thinking push adds element at start
- Expecting original array unchanged
- Confusing push with unshift
10 at the end of array nums:let nums = [1, 2, 3]; nums.push = 10; console.log(nums);
Solution
Step 1: Analyze the push usage
The code assigns 10 tonums.pushinstead of callingnums.push(10).Step 2: Understand consequences of assignment
This overwrites the push method with a number, so push no longer works as a function.Final Answer:
Using assignment instead of calling push as a function -> Option AQuick Check:
push must be called as a function, not assigned [OK]
- Assigning value to push instead of calling it
- Forgetting parentheses after push
- Assuming push is a property, not a method
arr? let arr = [10]; arr.push(20); arr.push(30, 40); arr.push();
Solution
Step 1: Trace each push call
Initial array: [10].arr.push(20)adds 20 -> [10, 20].arr.push(30, 40)adds both 30 and 40 -> [10, 20, 30, 40].arr.push()with no arguments adds nothing.Step 2: Confirm final array state
No undefined or nested arrays are added. Final array is [10, 20, 30, 40].Final Answer:
[10, 20, 30, 40] -> Option BQuick Check:
push() adds all args at end; no args means no change [OK]
- Thinking push() with no args adds undefined
- Assuming push(30, 40) adds an array instead of separate elements
- Confusing nested arrays with multiple arguments
Ready to prove it?
Pass the test and this pattern moves unknown
