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 nodes at the end
What is the printed state of the doubly linked list after inserting 10, 20, and 30 at the end?
DSA Typescript
class Node { data: number; next: Node | null = null; prev: Node | null = null; constructor(data: number) { this.data = data; } } class DoublyLinkedList { head: Node | null = null; insertAtEnd(data: number): void { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; newNode.prev = current; } printList(): string { let result = ''; let current = this.head; while (current) { result += current.data + ' <-> '; current = current.next; } return result + 'null'; } } const dll = new DoublyLinkedList(); dll.insertAtEnd(10); dll.insertAtEnd(20); dll.insertAtEnd(30); console.log(dll.printList());
Attempts:
2 left
💡 Hint
Remember that inserting at the end adds nodes after the last node.
✗ Incorrect
The insertAtEnd method adds nodes at the end, so the list grows as 10, then 20, then 30. The printList method shows nodes from head to tail.
❓ Predict Output
intermediateOutput after inserting into an empty list
What is the printed state of the doubly linked list after inserting a single node with value 5 at the end?
DSA Typescript
class Node { data: number; next: Node | null = null; prev: Node | null = null; constructor(data: number) { this.data = data; } } class DoublyLinkedList { head: Node | null = null; insertAtEnd(data: number): void { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; newNode.prev = current; } printList(): string { let result = ''; let current = this.head; while (current) { result += current.data + ' <-> '; current = current.next; } return result + 'null'; } } const dll = new DoublyLinkedList(); dll.insertAtEnd(5); console.log(dll.printList());
Attempts:
2 left
💡 Hint
Inserting into an empty list sets the head to the new node.
✗ Incorrect
Since the list was empty, the new node with value 5 becomes the head and tail. The print shows '5 <-> null'.
🔧 Debug
advancedIdentify the bug in insertAtEnd method
What error will occur when running this insertAtEnd method and why?
DSA Typescript
insertAtEnd(data: number): void {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
newNode.prev = current;
}Attempts:
2 left
💡 Hint
Check what happens when the list is empty and current is assigned.
✗ Incorrect
When the list is empty, head is set to newNode but code continues. current is assigned head (newNode), but since no return after setting head, the code tries to access current.next which is null, causing error.
❓ Predict Output
advancedOutput after multiple insertions and one removal
Given the code below, what is the printed state of the doubly linked list after inserting 1, 2, 3 at the end and then removing the node with value 2?
DSA Typescript
class Node { data: number; next: Node | null = null; prev: Node | null = null; constructor(data: number) { this.data = data; } } class DoublyLinkedList { head: Node | null = null; insertAtEnd(data: number): void { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; newNode.prev = current; } remove(data: number): void { let current = this.head; while (current) { if (current.data === data) { if (current.prev) { current.prev.next = current.next; } else { this.head = current.next; } if (current.next) { current.next.prev = current.prev; } return; } current = current.next; } } printList(): string { let result = ''; let current = this.head; while (current) { result += current.data + ' <-> '; current = current.next; } return result + 'null'; } } const dll = new DoublyLinkedList(); dll.insertAtEnd(1); dll.insertAtEnd(2); dll.insertAtEnd(3); dll.remove(2); console.log(dll.printList());
Attempts:
2 left
💡 Hint
Removing a node updates the previous and next links to skip the removed node.
✗ Incorrect
Node with value 2 is removed, so the list links 1 directly to 3. The print shows '1 <-> 3 <-> null'.
🧠 Conceptual
expertWhy is updating both next and prev pointers important in doubly linked list insertion?
When inserting a new node at the end of a doubly linked list, why must we update both the previous node's next pointer and the new node's prev pointer?
Attempts:
2 left
💡 Hint
Think about how doubly linked lists allow moving in both directions.
✗ Incorrect
Updating both pointers ensures the list can be traversed forward and backward without errors or broken links.
