Challenge - 5 Problems
Doubly Linked List Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting at position 2?
Consider a doubly linked list with nodes 10 <-> 20 <-> 30. We insert a new node with value 15 at position 2 (1-based index). What is the printed list after insertion?
DSA Typescript
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; insertAtPosition(value: number, position: number) { const newNode = new Node(value); if (position === 1) { newNode.next = this.head; if (this.head) this.head.prev = newNode; this.head = newNode; return; } let current = this.head; let count = 1; while (current && count < position - 1) { current = current.next; count++; } if (!current) return; // position out of bounds newNode.next = current.next; if (current.next) current.next.prev = newNode; current.next = newNode; newNode.prev = current; } printList() { let current = this.head; const values = []; while (current) { values.push(current.value); current = current.next; } console.log(values.join(' <-> ') + ' <-> null'); } } const dll = new DoublyLinkedList(); dll.head = new Node(10); dll.head.next = new Node(20); dll.head.next.prev = dll.head; dll.head.next.next = new Node(30); dll.head.next.next.prev = dll.head.next; dll.insertAtPosition(15, 2); dll.printList();
Attempts:
2 left
💡 Hint
Remember that position 2 means the new node goes after the first node.
✗ Incorrect
Inserting at position 2 means the new node with value 15 is placed between nodes with values 10 and 20. The list becomes 10 <-> 15 <-> 20 <-> 30.
🧠 Conceptual
intermediateWhat happens if we insert at position 1 in a non-empty doubly linked list?
In a doubly linked list, inserting a new node at position 1 means adding it at the head. What changes happen to the list pointers?
Attempts:
2 left
💡 Hint
Think about how the head pointer and the previous head node's pointers change.
✗ Incorrect
When inserting at position 1, the new node becomes the head. Its next pointer points to the old head, and the old head's prev pointer is updated to point back to the new node.
🔧 Debug
advancedWhy does this insertion code cause a runtime error?
Given the code below, why does calling insertAtPosition(25, 5) cause an error when the list has only 3 nodes?
DSA Typescript
insertAtPosition(value: number, position: number) {
const newNode = new Node(value);
if (position === 1) {
newNode.next = this.head;
if (this.head) this.head.prev = newNode;
this.head = newNode;
return;
}
let current = this.head;
let count = 1;
while (count < position - 1) {
current = current.next!;
count++;
}
newNode.next = current.next;
if (current.next) current.next.prev = newNode;
current.next = newNode;
newNode.prev = current;
}Attempts:
2 left
💡 Hint
Check what happens when position is greater than list length + 1.
✗ Incorrect
The while loop assumes current.next is not null, but when position is beyond list length + 1, current becomes null and accessing current.next causes a runtime error.
❓ Predict Output
advancedWhat is the list after inserting at the tail position?
Starting with list 5 <-> 10 <-> 15, insert 20 at position 4 (tail). What is the printed list?
DSA Typescript
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; insertAtPosition(value: number, position: number) { const newNode = new Node(value); if (position === 1) { newNode.next = this.head; if (this.head) this.head.prev = newNode; this.head = newNode; return; } let current = this.head; let count = 1; while (current && count < position - 1) { current = current.next; count++; } if (!current) return; newNode.next = current.next; if (current.next) current.next.prev = newNode; current.next = newNode; newNode.prev = current; } printList() { let current = this.head; const values = []; while (current) { values.push(current.value); current = current.next; } console.log(values.join(' <-> ') + ' <-> null'); } } const dll = new DoublyLinkedList(); dll.head = new Node(5); dll.head.next = new Node(10); dll.head.next.prev = dll.head; dll.head.next.next = new Node(15); dll.head.next.next.prev = dll.head.next; dll.insertAtPosition(20, 4); dll.printList();
Attempts:
2 left
💡 Hint
Position 4 means after the third node, at the tail.
✗ Incorrect
Inserting at position 4 adds the new node after the third node (15), making 20 the new tail.
🧠 Conceptual
expertWhat is the time complexity of inserting at a specific position in a doubly linked list?
Given a doubly linked list of length n, what is the time complexity to insert a node at position k (1 <= k <= n+1)?
Attempts:
2 left
💡 Hint
Think about how we find the position before inserting.
✗ Incorrect
To insert at position k, we must traverse from the head to the (k-1)th node, which takes O(k) time. Doubly linked lists do not support direct access by index.
