Challenge - 5 Problems
Doubly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting 10 at the beginning?
Consider a doubly linked list initially containing nodes with values 20 and 30. We insert a new node with value 10 at the beginning. What will be the printed list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { constructor() { this.head = null; } insertAtBeginning(data) { const newNode = new Node(data); if (this.head === null) { this.head = newNode; return; } newNode.next = this.head; this.head.prev = newNode; this.head = newNode; } 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.insertAtBeginning(20); dll.insertAtBeginning(30); dll.insertAtBeginning(10); dll.printList();
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the new node becomes the head, pushing existing nodes to the right.
✗ Incorrect
The insertAtBeginning method adds the new node before the current head. The new node's next points to the old head, and the old head's prev points back to the new node. So the order after inserting 20, then 30, then 10 at beginning is 10 -> 30 -> 20 -> null.
❓ Predict Output
intermediateWhat is the output after inserting 5 at the beginning of an empty list?
We start with an empty doubly linked list and insert a node with value 5 at the beginning. What will be the printed list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { constructor() { this.head = null; } insertAtBeginning(data) { const newNode = new Node(data); if (this.head === null) { this.head = newNode; return; } newNode.next = this.head; this.head.prev = newNode; this.head = newNode; } 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.insertAtBeginning(5); dll.printList();
Attempts:
2 left
💡 Hint
When the list is empty, the new node becomes the only node and head.
✗ Incorrect
Since the list is empty, the new node with value 5 becomes the head and the only node. So the printed list is 5 -> null.
🔧 Debug
advancedIdentify the error in this insertAtBeginning method
The following insertAtBeginning method is intended to add a new node at the start of a doubly linked list. What error will it cause when called on a non-empty list?
DSA Javascript
insertAtBeginning(data) {
const newNode = new Node(data);
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
newNode.prev = this.head;
}Attempts:
2 left
💡 Hint
Check the last line where newNode.prev is assigned.
✗ Incorrect
The last line sets newNode.prev = this.head, but this.head was just set to newNode itself, so newNode.prev points to itself. This creates a circular reference causing infinite loops when traversing backward.
❓ Predict Output
advancedWhat is the output after multiple insertions at beginning?
Starting with an empty doubly linked list, we insert 1, then 2, then 3 at the beginning. What will be the printed list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { constructor() { this.head = null; } insertAtBeginning(data) { const newNode = new Node(data); if (this.head === null) { this.head = newNode; return; } newNode.next = this.head; this.head.prev = newNode; this.head = newNode; } 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.insertAtBeginning(1); dll.insertAtBeginning(2); dll.insertAtBeginning(3); dll.printList();
Attempts:
2 left
💡 Hint
Each new insertion at beginning becomes the new head.
✗ Incorrect
Inserting 1 first makes list: 1 -> null. Then inserting 2 at beginning makes: 2 -> 1 -> null. Then inserting 3 at beginning makes: 3 -> 2 -> 1 -> null.
🧠 Conceptual
expertWhy is updating the prev pointer important when inserting at beginning?
When inserting a new node at the beginning of a doubly linked list, why must we update the previous head node's prev pointer to the new node?
Attempts:
2 left
💡 Hint
Think about how doubly linked lists allow moving backward.
✗ Incorrect
The prev pointer of the old head must point to the new node to maintain correct backward links. Without this, traversing backward from tail would fail or give incorrect results.
