Array Insertion at End in DSA Typescript - Time & Space Complexity
We want to understand how long it takes to add an item at the end of an array.
How does the time needed change as the array grows bigger?
Analyze the time complexity of the following code snippet.
function insertAtEnd(arr: number[], value: number): void {
arr[arr.length] = value;
}
This code adds a new value at the end of the array by placing it at the current length index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single direct assignment to the array at the end index.
- How many times: Exactly once per insertion.
Adding one item at the end takes the same small step no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant even as the array grows.
Time Complexity: O(1)
This means adding an item at the end takes the same short time no matter how many items are already there.
[X] Wrong: "Adding at the end takes longer as the array grows because it has to move all items."
[OK] Correct: The insertion just places the new item at the next free spot without moving existing items.
Knowing that adding at the end is quick helps you choose the right data structure and explain your choices clearly.
"What if the array needs to resize its storage when full? How would the time complexity change then?"
Practice
Solution
Step 1: Understand array insertion
Inserting at the end means adding a new element after the last existing element.Step 2: Effect on array
The new element becomes the last item, increasing the array size by one.Final Answer:
Adds the element as the last item in the array -> Option AQuick Check:
Insertion at end = append last [OK]
- Confusing insertion at end with removal
- Thinking insertion replaces elements
- Assuming insertion sorts the array
Solution
Step 1: Identify correct method for insertion at end
In TypeScript (JavaScript), the method to add at the end is `push()`.Step 2: Check other options
Methods `add()`, `insert()`, and `append()` do not exist on arrays in TypeScript.Final Answer:
arr.push(5); -> Option CQuick Check:
Use push() to add at end [OK]
- Using non-existent methods like add or append
- Confusing push() with pop()
- Trying to assign directly to arr[arr.length]
let arr = [1, 2, 3]; arr.push(4); console.log(arr);
Solution
Step 1: Initial array state
Array starts as [1, 2, 3].Step 2: Apply push(4)
push(4) adds 4 at the end, so array becomes [1, 2, 3, 4].Final Answer:
[1, 2, 3, 4] -> Option BQuick Check:
push adds element at end [OK]
- Thinking push adds at start
- Confusing push with unshift
- Expecting array to remain unchanged
let nums = [1, 2, 3]; nums.push = 10; console.log(nums);
Solution
Step 1: Analyze push usage
The code assigns 10 to nums.push instead of calling nums.push(10).Step 2: Effect of assignment
This overwrites the push method, so no insertion happens and push is lost.Final Answer:
Using assignment instead of calling push method -> Option AQuick Check:
push must be called as a function [OK]
- Assigning value to push instead of calling it
- Forgetting parentheses after push
- Assuming push is a property, not a method
Solution
Step 1: Understand loop range and insertion
We want to insert 1, 2, 3 at the end, so loop from 1 to 3 inclusive.Step 2: Check each option
A pushes 0,1,2 instead of 1,2,3.
B assigns directly to indices 1,2,3 leaving index 0 empty.
C uses push with i from 1 to 3, correctly adding 1,2,3.
D loops from 1 to 2 only, missing 3.Final Answer:
for(let i = 1; i <= 3; i++) { data.push(i); } -> Option DQuick Check:
Loop 1 to 3 with push() adds correct elements [OK]
- Using wrong loop range
- Assigning to array indices instead of push
- Starting loop at 0 instead of 1
Ready to prove it?
Pass the test and this pattern moves unknown
