Insert at Middle Specific Position in DSA Javascript - Time & Space Complexity
When inserting an element at a specific middle position in a list, it is important to understand how the time taken changes as the list grows.
We want to know how the number of steps grows when the list gets bigger.
Analyze the time complexity of the following code snippet.
function insertAtMiddle(arr, element, position) {
for (let i = arr.length; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = element;
return arr;
}
This code inserts an element into an array at a given middle position by shifting elements to the right.
- Primary operation: The for-loop that shifts elements one position 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 array size grows, the number of shifts grows roughly in proportion to how far the insertion point is from the end.
| Input Size (n) | Approx. Operations (shifts) |
|---|---|
| 10 | About 5 if inserting in middle |
| 100 | About 50 if inserting in middle |
| 1000 | About 500 if inserting in middle |
Pattern observation: The number of operations grows roughly linearly with the size of the array.
Time Complexity: O(n)
This means the time to insert grows in a straight line as the array gets bigger.
[X] Wrong: "Inserting in the middle is always very fast because we just put the element there."
[OK] Correct: Actually, we must move all elements after the insertion point to make space, which takes time proportional to how many elements are shifted.
Understanding how insertion time grows helps you explain and reason about array operations clearly, a useful skill in many coding discussions.
"What if we changed the data structure from an array to a linked list? How would the time complexity for insertion 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 index, shifting others if needed.Step 2: Differentiate from other operations
Insertion at start or end is different; here we insert inside the list at the given position.Final Answer:
Adding a new node exactly at the given position counting from the start -> Option CQuick Check:
Insert at position = Add node there [OK]
- Confusing insertion with deletion
- Thinking insertion only happens at start or end
- Ignoring position counting from start
Solution
Step 1: Identify common method naming
Common method to insert at a position is named insertAt(position, node).Step 2: Match parameters order
Position comes first, then the node to insert, matching list.insertAt(3, newNode).Final Answer:
list.insertAt(3, newNode); -> Option DQuick Check:
insertAt(position, node) syntax [OK]
- Swapping parameter order
- Using wrong method names
- Confusing insertAt with push or addNode
function insertAtPosition(head, value, position) {
let newNode = { value: value, next: null };
if (position === 1) {
newNode.next = head;
return newNode;
}
let current = head;
let count = 1;
while (count < position - 1 && current != null) {
current = current.next;
count++;
}
if (current == null) return head;
newNode.next = current.next;
current.next = newNode;
return head;
}
let head = { value: 10, next: { value: 20, next: { value: 30, next: null } } };
head = insertAtPosition(head, 99, 2);
// Print list
let result = [];
let temp = head;
while(temp) {
result.push(temp.value);
temp = temp.next;
}
console.log(result);Solution
Step 1: Locate position 2 in the list
Position 2 means after first node (10), before second node (20).Step 2: Insert new node with value 99 at position 2
New node points to 20, first node points to new node, resulting list: 10 -> 99 -> 20 -> 30.Final Answer:
[10, 99, 20, 30] -> Option BQuick Check:
Insert 99 at pos 2 shifts 20 right [OK]
- Inserting at wrong position
- Not updating next pointers correctly
- Confusing position counting starting at 0
function insertAtPosition(head, value, position) {
let newNode = { value: value, next: null };
if (position === 1) {
newNode.next = head;
return newNode;
}
let current = head;
let count = 1;
while (count < position - 1) {
current = current.next;
count++;
}
newNode.next = current.next;
current.next = newNode;
return head;
}Solution
Step 1: Analyze while loop condition
The loop runs without checking if current is null, so if position > list length + 1, current becomes null and next access fails.Step 2: Identify missing boundary check
There is no check to stop loop if current is null, causing runtime error when accessing current.next.Final Answer:
The while loop can cause an error if position is greater than list length + 1 -> Option AQuick Check:
Loop needs null check to avoid error [OK]
- Ignoring null checks in loops
- Assuming position is always valid
- Not returning head after insertion
function insertAtPosition(head, value, position) {
let newNode = { value: value, next: null };
if (position === 1) {
newNode.next = head;
return newNode;
}
let current = head;
let count = 1;
while (count < position - 1 && current != null) {
current = current.next;
count++;
}
if (current == null) return head;
newNode.next = current.next;
current.next = newNode;
return head;
}
let head = { value: 5, next: { value: 10, next: { value: 15, next: { value: 20, next: null } } } };
head = insertAtPosition(head, 12, 3);
// Print list
let result = [];
let temp = head;
while(temp) {
result.push(temp.value);
temp = temp.next;
}
console.log(result);
What is the output?Solution
Step 1: Identify position 3 in the list
Position 3 means after nodes 5 (pos 1) and 10 (pos 2), so between 10 and 15.Step 2: Insert new node with value 12 at position 3
New node points to 15, node 10 points to new node, resulting list: 5 -> 10 -> 12 -> 15 -> 20.Final Answer:
[5, 10, 12, 15, 20] -> Option AQuick Check:
Insert 12 at pos 3 shifts 15 right [OK]
- Inserting at wrong position
- Not updating next pointers correctly
- Confusing position indexing starting at 0
Ready to prove it?
Pass the test and this pattern moves unknown
