Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Doubly Linked List after Insertions
What is the printed state of the doubly linked list after inserting nodes with values 10, 20, and 30 at the end?
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; return; } 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
Remember that append adds nodes at the end, so the order is the same as insertion.
✗ Incorrect
Appending nodes adds them to the tail, so the list prints from head to tail as 10 <-> 20 <-> 30 <-> null.
❓ Predict Output
intermediateOutput after Removing Head Node
Given a doubly linked list with nodes 5 <-> 15 <-> 25 <-> null, what is the printed list after removing 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; return; } this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } removeHead() { if (!this.head) return; if (this.head === this.tail) { this.head = null; this.tail = null; return; } 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.removeHead(); dll.printList();
Attempts:
2 left
💡 Hint
Removing head moves the head pointer to the next node and updates prev to null.
✗ Incorrect
After removing the head (5), the new head is 15, and the list is 15 <-> 25 <-> null.
🔧 Debug
advancedIdentify the Bug in Node Insertion at Head
What error will occur when running this code that inserts a new node at the head of a doubly linked list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } insertAtHead(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; } } 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.insertAtHead(1); dll.insertAtHead(2); dll.insertAtHead(3); dll.printList(); console.log(dll.tail.prev.value);
Attempts:
2 left
💡 Hint
Check how tail and prev pointers are updated during insertions at head.
✗ Incorrect
The list after insertions is 3 <-> 2 <-> 1 <-> null. The tail is 1, and tail.prev is 2, so dll.tail.prev.value is 2.
❓ Predict Output
advancedOutput after Reversing a Doubly Linked List
What is the printed state of the doubly linked list after reversing it? Initial list: 1 <-> 4 <-> 7 <-> null
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; return; } 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(4); dll.append(7); dll.reverse(); dll.printList();
Attempts:
2 left
💡 Hint
Reversing swaps next and prev pointers and updates head to the last node.
✗ Incorrect
After reversing, the list order is reversed: 7 <-> 4 <-> 1 <-> null.
🧠 Conceptual
expertUnderstanding Node Connections in Doubly Linked List
In a doubly linked list, if a node's next pointer is null but its prev pointer is not null, which node is it most likely to be?
Attempts:
2 left
💡 Hint
Think about which node has no next node but has a previous node.
✗ Incorrect
The tail node has no next node (next is null) but has a previous node (prev not null).
