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 nodes in a doubly linked list?
Consider the following JavaScript code that creates a doubly linked list and inserts three nodes. What will be the printed output showing the list from head to tail?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' <-> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(10); dll.append(20); dll.append(30); dll.printList();
Attempts:
2 left
💡 Hint
Trace the append method and how nodes link forward and backward.
✗ Incorrect
The append method adds nodes at the tail. The printList method prints from head to tail using next pointers, so the output is 10 <-> 20 <-> 30 <-> null.
❓ Predict Output
intermediateWhat is the output after deleting the head node in a doubly linked list?
Given the doubly linked list with nodes 5 <-> 15 <-> 25, what will be the printed output after deleting the head node?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } deleteHead() { if (!this.head) return; if (this.head === this.tail) { this.head = null; this.tail = null; } else { this.head = this.head.next; this.head.prev = null; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' <-> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(5); dll.append(15); dll.append(25); dll.deleteHead(); dll.printList();
Attempts:
2 left
💡 Hint
Deleting head moves head pointer to next node and updates prev to null.
✗ Incorrect
After deleting the head (5), the new head is 15 and its prev pointer is set to null. The list prints 15 <-> 25 <-> null.
🔧 Debug
advancedIdentify the bug in the doubly linked list insertion method
The following code tries to insert a new node at the beginning of a doubly linked list. What is the bug that will cause incorrect linking?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } insertAtBeginning(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { newNode.next = this.head; this.head.prev = newNode; this.head = newNode; this.tail.prev = newNode; // Bug here } } }
Attempts:
2 left
💡 Hint
Tail's prev pointer should not change when inserting at head.
✗ Incorrect
The line 'this.tail.prev = newNode;' wrongly changes the tail's prev pointer, corrupting the list. It should be removed.
❓ Predict Output
advancedWhat is the output after reversing a doubly linked list?
Given the doubly linked list 1 <-> 2 <-> 3 <-> null, what will be the printed output after reversing the list using the code below?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } reverse() { let current = this.head; let temp = null; while (current) { temp = current.prev; current.prev = current.next; current.next = temp; current = current.prev; } if (temp) { this.head = temp.prev; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' <-> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(1); dll.append(2); dll.append(3); dll.reverse(); dll.printList();
Attempts:
2 left
💡 Hint
Reversing swaps next and prev pointers and updates head.
✗ Incorrect
The reverse method swaps next and prev for each node and updates head to the old tail, so the list prints 3 <-> 2 <-> 1 <-> null.
🧠 Conceptual
expertHow many pointers are updated when inserting a node in the middle of a doubly linked list?
When inserting a new node between two existing nodes in a doubly linked list, how many pointers must be updated to maintain correct links?
Attempts:
2 left
💡 Hint
Consider next and prev pointers of the new node and its neighbors.
✗ Incorrect
Inserting in the middle requires updating four pointers: newNode.next, newNode.prev, previousNode.next, and nextNode.prev.
