Challenge - 5 Problems
Insert at Middle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of inserting a node at position 3 in a linked list
What is the printed state of the linked list after inserting a node with value 99 at position 3?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtPosition(value, position) { const newNode = new Node(value); if (position === 1) { newNode.next = this.head; this.head = newNode; return; } let current = this.head; let count = 1; while (current !== null && count < position - 1) { current = current.next; count++; } if (current === null) return; // position out of bounds newNode.next = current.next; current.next = newNode; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.insertAtPosition(10, 1); list.insertAtPosition(20, 2); list.insertAtPosition(30, 3); list.insertAtPosition(40, 4); list.insertAtPosition(99, 3); list.printList();
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node should be the third node in the list.
✗ Incorrect
The new node with value 99 is inserted after the second node (value 20), so it becomes the third node.
❓ Predict Output
intermediateResult of inserting at position beyond list length
What is the printed state of the linked list after trying to insert a node with value 50 at position 10 in a list of 4 nodes?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtPosition(value, position) { const newNode = new Node(value); if (position === 1) { newNode.next = this.head; this.head = newNode; return; } let current = this.head; let count = 1; while (current !== null && count < position - 1) { current = current.next; count++; } if (current === null) return; // position out of bounds newNode.next = current.next; current.next = newNode; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.insertAtPosition(1, 1); list.insertAtPosition(2, 2); list.insertAtPosition(3, 3); list.insertAtPosition(4, 4); list.insertAtPosition(50, 10); list.printList();
Attempts:
2 left
💡 Hint
Inserting at a position beyond the list length does not change the list.
✗ Incorrect
Since position 10 is beyond the current list length, the insertion is ignored and the list remains unchanged.
🔧 Debug
advancedIdentify the error in insertAtPosition method
What error will this code produce when inserting at position 1 in an empty list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtPosition(value, position) { const newNode = new Node(value); if (position === 1) { this.head.next = newNode; this.head = newNode; return; } let current = this.head; let count = 1; while (current !== null && count < position - 1) { current = current.next; count++; } if (current === null) return; newNode.next = current.next; current.next = newNode; } } const list = new LinkedList(); list.insertAtPosition(5, 1);
Attempts:
2 left
💡 Hint
Check what happens when this.head is null and you try to access this.head.next.
✗ Incorrect
When the list is empty, this.head is null, so accessing this.head.next causes a TypeError.
❓ Predict Output
advancedOutput after multiple insertions at middle positions
What is the printed state of the linked list after these insertions?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtPosition(value, position) { const newNode = new Node(value); if (position === 1) { newNode.next = this.head; this.head = newNode; return; } let current = this.head; let count = 1; while (current !== null && count < position - 1) { current = current.next; count++; } if (current === null) return; newNode.next = current.next; current.next = newNode; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.insertAtPosition(1, 1); list.insertAtPosition(3, 2); list.insertAtPosition(2, 2); list.insertAtPosition(5, 4); list.insertAtPosition(4, 4); list.printList();
Attempts:
2 left
💡 Hint
Insertions at position 2 and 4 shift existing nodes to the right.
✗ Incorrect
Nodes are inserted at positions 2 and 4, resulting in the sorted order 1 -> 2 -> 3 -> 4 -> 5 -> null.
🧠 Conceptual
expertTime complexity of inserting at a specific middle position in singly linked list
What is the time complexity of inserting a node at a specific middle position in a singly linked list of length n?
Attempts:
2 left
💡 Hint
Think about how you find the node before the insertion point.
✗ Incorrect
You must traverse the list from the head to the node before the insertion position, which takes O(n) time.
