Insert at Middle Specific Position in DSA Java - Time & Space Complexity
When we insert a new item at a specific middle position in a list, we want to know how the time it takes 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.
public void insertAtPosition(int[] arr, int n, int pos, int val) {
for (int i = n - 1; i >= pos; i--) {
arr[i + 1] = arr[i];
}
arr[pos] = val;
}
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.
- How many times: It runs from the end of the list down to the insertion position, so roughly n - pos times.
As the list gets bigger, the number of shifts depends on where we insert.
| Input Size (n) | Approx. Operations (shifts) |
|---|---|
| 10 | Up to 9 shifts |
| 100 | Up to 99 shifts |
| 1000 | Up to 999 shifts |
Pattern observation: The work grows roughly in direct proportion to the size of the list when inserting near the start.
Time Complexity: O(n)
This means the time to insert grows linearly with the list size because shifting elements takes more steps as the list grows.
[X] Wrong: "Inserting at any position always takes the same time as adding at the end."
[OK] Correct: Inserting in the middle or start requires moving many elements, which takes more time than just adding at the end.
Understanding how insertion time changes with list size helps you explain and improve data handling in real projects.
"What if we used a linked list instead of an array? How would the time complexity of insertion at a middle position change?"
Practice
Solution
Step 1: Understand position-based insertion
Inserting at a specific position means placing the new node exactly at that index, shifting others if needed.Step 2: Differentiate from other insertions
Start or end insertions are special cases, but middle insertion targets a specific index inside the list.Final Answer:
Adding a new node exactly at the given position counting from the head -> Option AQuick Check:
Insert at position = Insert node at that index [OK]
- Confusing insertion with replacement
- Thinking insertion only happens at start or end
- Ignoring zero-based or one-based indexing
Solution
Step 1: Identify position 3 in zero-based indexing
Position 3 means after two nodes: head -> next -> next, so newNode.next should point to head.next.next.Step 2: Link newNode correctly
Set newNode.next to head.next.next, then update head.next.next to newNode to insert at position 3.Final Answer:
Node newNode = new Node(10); newNode.next = head.next.next; head.next.next = newNode; -> Option CQuick Check:
Insert at position 3 = link newNode after second node [OK]
- Incorrectly setting newNode.next to wrong node
- Not updating previous node's next pointer
- Confusing position indexing
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
// Insert 25 at position 2
Node newNode = new Node(25);
Node temp = head;
for (int i = 1; i < 2; i++) {
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
Solution
Step 1: Traverse to node at position 1
The loop runs once (i=1), so temp points to node with value 20.Step 2: Insert newNode after temp
newNode.next points to temp.next (node 30), then temp.next points to newNode, inserting 25 after 20.Final Answer:
10 -> 20 -> 30 -> 25 -> 40 -> null -> Option BQuick Check:
Insert at position 2 places 25 after 20 [OK]
- Looping one extra time causing wrong insertion
- Not updating newNode.next correctly
- Confusing position indexing
Node newNode = new Node(15);
Node temp = head;
for (int i = 0; i < 1; i++) {
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;Solution
Step 1: Analyze loop movement
The loop runs once, moving temp from head to head.next, so temp points to node at position 1, not before it.Step 2: Understand insertion point
To insert at position 1, temp should point to node before position 1 (head), but here it points to position 1 node, causing incorrect insertion.Final Answer:
Loop moves temp too far, skipping the head node -> Option AQuick Check:
Loop should stop before position to insert [OK]
- Looping one time too many
- Confusing position indexing
- Not handling insertion at position 0 separately
Node newNode = new Node(12);
Node temp = head;
int position = 3;
for (int i = 1; i < position - 1; i++) {
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
// Print list
Node current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.print("null");Solution
Step 1: Loop to node before position 3
The loop runs with i=1 only, so temp moves from head (5) to next (10), which is node before position 3.Step 2: Insert newNode after temp
newNode.next points to temp.next (15), then temp.next points to newNode, inserting 12 between 10 and 15.Final Answer:
5 -> 10 -> 12 -> 15 -> 20 -> null -> Option DQuick Check:
Insert at position 3 places 12 after 10 [OK]
- Looping too many times causing wrong insertion
- Not updating newNode.next correctly
- Confusing position indexing
Ready to prove it?
Pass the test and this pattern moves unknown
