Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting at position 2
What is the printed state of the doubly linked list after inserting value 99 at position 2?
DSA Javascript
class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { constructor() { this.head = null; } insertAtPosition(data, position) { const newNode = new Node(data); 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; let result = ''; while (current) { result += current.data + ' <-> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.insertAtPosition(10, 1); dll.insertAtPosition(20, 2); dll.insertAtPosition(30, 3); dll.insertAtPosition(99, 2); dll.printList();
Attempts:
2 left
💡 Hint
Remember that position 1 means inserting at the head, and position 2 means after the first node.
✗ Incorrect
Inserting 99 at position 2 places it between the first node (10) and the second node (20). So the list becomes 10 <-> 99 <-> 20 <-> 30 <-> null.
❓ Predict Output
intermediateOutput after inserting at head position
What is the printed state of the doubly linked list after inserting value 5 at position 1?
DSA Javascript
class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { constructor() { this.head = null; } insertAtPosition(data, position) { const newNode = new Node(data); 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; let result = ''; while (current) { result += current.data + ' <-> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.insertAtPosition(10, 1); dll.insertAtPosition(20, 2); dll.insertAtPosition(30, 3); dll.insertAtPosition(5, 1); dll.printList();
Attempts:
2 left
💡 Hint
Inserting at position 1 means adding at the start of the list.
✗ Incorrect
The new node with value 5 becomes the new head, pushing the existing nodes forward.
🔧 Debug
advancedIdentify the error in insertAtPosition method
What error will occur when trying to insert at a position greater than the list length using this code?
DSA Javascript
class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { constructor() { this.head = null; } insertAtPosition(data, position) { const newNode = new Node(data); 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++; } // Missing check if current is null here newNode.next = current.next; if (current.next) current.next.prev = newNode; current.next = newNode; newNode.prev = current; } } const dll = new DoublyLinkedList(); dll.insertAtPosition(10, 1); dll.insertAtPosition(20, 2); dll.insertAtPosition(30, 3); dll.insertAtPosition(40, 5);
Attempts:
2 left
💡 Hint
Check what happens if position is beyond the list length and current becomes null.
✗ Incorrect
If position is greater than list length + 1, current becomes null and accessing current.next causes a TypeError.
❓ Predict Output
advancedOutput after inserting at last position
What is the printed state of the doubly linked list after inserting value 40 at position 4?
DSA Javascript
class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { constructor() { this.head = null; } insertAtPosition(data, position) { const newNode = new Node(data); 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; let result = ''; while (current) { result += current.data + ' <-> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.insertAtPosition(10, 1); dll.insertAtPosition(20, 2); dll.insertAtPosition(30, 3); dll.insertAtPosition(40, 4); dll.printList();
Attempts:
2 left
💡 Hint
Inserting at position 4 means adding after the third node, which is the current last node.
✗ Incorrect
The new node with value 40 is added at the end, after 30.
🧠 Conceptual
expertWhy is updating prev pointer important in doubly linked list insertion?
When inserting a new node in a doubly linked list, why must we update the prev pointer of the node after the inserted node?
Attempts:
2 left
💡 Hint
Think about how doubly linked lists allow moving backwards.
✗ Incorrect
The prev pointer links nodes backward. Without updating it, backward traversal breaks.
