Insert at Middle Specific Position in DSA Typescript - Time & Space Complexity
When we insert an element at a specific middle position in a list, we want to know how the time needed changes as the list grows.
We ask: How much work does the computer do when the list gets bigger?
Analyze the time complexity of the following code snippet.
function insertAtMiddle(arr: number[], value: number, position: number): number[] {
for (let i = arr.length; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = value;
return arr;
}
This code inserts a value at a given position by shifting elements to the right to make space.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that shifts elements one by one to the right.
- How many times: It runs from the end of the array down to the insertion position, so roughly n - position times.
As the list grows, the number of shifts depends on how far the insertion point is from the end.
| Input Size (n) | Approx. Operations (shifts) |
|---|---|
| 10 | About 5 if inserted in middle |
| 100 | About 50 if inserted in middle |
| 1000 | About 500 if inserted in middle |
Pattern observation: The work grows roughly in direct proportion to the size of the list.
Time Complexity: O(n)
This means the time to insert grows linearly as the list gets bigger.
[X] Wrong: "Inserting in the middle is always fast and takes constant time."
[OK] Correct: Because elements after the insertion point must move, the time depends on how many elements shift, which grows with list size.
Understanding how insertion time grows helps you explain trade-offs in data structures and shows you can think about efficiency clearly.
"What if we changed the data structure to a linked list? How would the time complexity of inserting at a middle position change?"
Practice
Solution
Step 1: Understand insertion position
Inserting at a specific middle position means placing the new node exactly at that position counting from the start of the list.Step 2: Differentiate from other insertions
Insertion at beginning or end is different; here we insert inside the list at a given index.Final Answer:
Adding a new node exactly at the given position counting from the start -> Option DQuick Check:
Insert at position = Add node at that index [OK]
- Confusing insertion with deletion
- Thinking insertion only happens at start or end
- Ignoring the position index
Solution
Step 1: Analyze loop for position
The loop runs from 1 to pos-2 to reach node before insertion point (pos-1).Step 2: Insert new node correctly
New node's next points to current.next, then current.next updated to new node.Final Answer:
Code in option A correctly inserts at position 2 -> Option AQuick Check:
Loop to pos-1, link new node [OK]
- Looping too far or too short
- Not updating newNode.next correctly
- Inserting at wrong position index
function insertAtPosition(head, val, pos) {
let newNode = {value: val, next: null};
if (pos === 1) {
newNode.next = head;
return newNode;
}
let current = head;
for (let i = 1; i < pos - 1; i++) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
return head;
}
// Initial list: 1 -> 2 -> 3 -> 4 -> null
// Insert 99 at position 3
let head = {value:1, next:{value:2, next:{value:3, next:{value:4, next:null}}}};
head = insertAtPosition(head, 99, 3);Solution
Step 1: Traverse to node before position 3
Loop runs i=1 to pos-2=1, so current moves to node with value 2.Step 2: Insert new node after current
New node 99 points to current.next (node 3), current.next updated to new node.Final Answer:
1 -> 2 -> 99 -> 3 -> 4 -> null -> Option BQuick Check:
Insert at pos 3 places 99 after 2 [OK]
- Inserting at wrong position
- Not updating links correctly
- Confusing position indexing
function insertAtPosition(head, val, pos) {
let newNode = {value: val, next: null};
let current = head;
for(let i = 0; i < pos - 1; i++) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
return head;
}Solution
Step 1: Analyze loop range
The loop runs from i=0 to pos-2, moving current pos-1 times, which moves current too far.Step 2: Consequence of loop
Current becomes the node at position pos, so current.next is undefined, causing error on insertion.Final Answer:
The loop runs one step too many, causing current to be null before insertion -> Option AQuick Check:
Loop overshoot breaks insertion [OK]
- Looping to position instead of position-1
- Not checking for position 1 insertion
- Incorrectly updating links
Solution
Step 1: Traverse safely
Move current node forward until either position-1 steps or current.next is null (end of list).Step 2: Insert at end if position too large
If position is beyond length, current.next will be null, so insert new node at end by setting current.next to new node.Final Answer:
Traverse until current.next is null or position reached, then insert new node at current.next -> Option CQuick Check:
Insert at end if position too large [OK]
- Not handling position beyond length
- Throwing error unnecessarily
- Inserting at wrong place when position invalid
Ready to prove it?
Pass the test and this pattern moves unknown
