Challenge - 5 Problems
Doubly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of inserting nodes at the end of a doubly linked list
What is the printed state of the doubly linked list after inserting 10, 20, and 30 at the end?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; } insertAtEnd(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let temp = this.head; while (temp.next) { temp = temp.next; } temp.next = newNode; newNode.prev = temp; } printList() { let temp = this.head; let result = ''; while (temp) { result += temp.data + ' <-> '; temp = temp.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.insertAtEnd(10); dll.insertAtEnd(20); dll.insertAtEnd(30); dll.printList();
Attempts:
2 left
💡 Hint
Remember that inserting at the end adds nodes after the last node, preserving order.
✗ Incorrect
The insertAtEnd method adds nodes to the end of the list. After inserting 10, 20, and 30, the list is 10 <-> 20 <-> 30 <-> null.
🧠 Conceptual
intermediateUnderstanding pointers in doubly linked list insertion
When inserting a new node at the end of a doubly linked list, which pointers must be updated?
Attempts:
2 left
💡 Hint
Think about how the new node connects to the existing last node.
✗ Incorrect
The new node's prev pointer must point to the last node, and the last node's next pointer must point to the new node to maintain the doubly linked structure.
🔧 Debug
advancedIdentify the bug in this insertAtEnd method
What error will this code produce when inserting nodes at the end of a doubly linked list?
DSA Javascript
insertAtEnd(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let temp = this.head;
while (temp.next) {
temp = temp.next;
}
temp.next = newNode;
newNode.prev = temp;
}Attempts:
2 left
💡 Hint
Check what happens when the list is empty and the code continues after setting head.
✗ Incorrect
When the list is empty, after setting head to newNode without returning, the code continues: temp = head (newNode), while(temp.next) skips (null), then temp.next = newNode (self-loop). Subsequent insertions cause infinite loop in while(temp.next) because of the self-loop (temp.next never falsy).
❓ Predict Output
advancedOutput after inserting nodes and then traversing backwards
After inserting 5, 15, and 25 at the end, what is the output of traversing the list backwards from the last node?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; } insertAtEnd(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let temp = this.head; while (temp.next) { temp = temp.next; } temp.next = newNode; newNode.prev = temp; } printReverse() { let temp = this.head; if (!temp) { console.log('null'); return; } while (temp.next) { temp = temp.next; } let result = ''; while (temp) { result += temp.data + ' <-> '; temp = temp.prev; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.insertAtEnd(5); dll.insertAtEnd(15); dll.insertAtEnd(25); dll.printReverse();
Attempts:
2 left
💡 Hint
Traversing backwards starts from the last node and follows prev pointers.
✗ Incorrect
The printReverse method moves to the last node, then prints nodes going backwards using prev pointers, resulting in 25 <-> 15 <-> 5 <-> null.
🧠 Conceptual
expertTime complexity of inserting at the end in a doubly linked list without tail pointer
What is the time complexity of inserting a node at the end of a doubly linked list if the list does NOT maintain a tail pointer?
Attempts:
2 left
💡 Hint
Think about how you find the last node without a tail pointer.
✗ Incorrect
Without a tail pointer, you must start at the head and traverse all nodes to reach the end, which takes O(n) time.
