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 Typescript
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; tail: Node | null = null; insertEnd(value: number) { 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.insertEnd(10); dll.insertEnd(20); dll.insertEnd(30); dll.printList();
Attempts:
2 left
💡 Hint
Remember that insertEnd adds nodes at the tail, so the order is the same as insertion.
✗ Incorrect
Nodes are inserted at the end, so the list grows as 10 <-> 20 <-> 30 <-> null.
❓ Predict Output
intermediateState of Doubly Linked List after Deletion
Given a doubly linked list with nodes 5 <-> 15 <-> 25 <-> 35 <-> null, what is the list after deleting the node with value 25?
DSA Typescript
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; tail: Node | null = null; insertEnd(value: number) { 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; } deleteValue(value: number) { let current = this.head; while (current) { if (current.value === value) { if (current.prev) { current.prev.next = current.next; } else { this.head = current.next; } if (current.next) { current.next.prev = current.prev; } else { this.tail = current.prev; } return; } current = current.next; } } 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.insertEnd(5); dll.insertEnd(15); dll.insertEnd(25); dll.insertEnd(35); dll.deleteValue(25); dll.printList();
Attempts:
2 left
💡 Hint
Deleting 25 removes it and links 15 directly to 35.
✗ Incorrect
Node with value 25 is removed, so 15's next points to 35 and 35's prev points to 15.
🔧 Debug
advancedIdentify the Bug in Node Insertion at Head
What error will occur when running this code that inserts a node at the head of a doubly linked list?
DSA Typescript
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; tail: Node | null = null; insertHead(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } newNode.next = this.head; this.head.prev = newNode; this.head = newNode; } } const dll = new DoublyLinkedList(); dll.insertHead(100); dll.insertHead(200); console.log(dll.head!.prev!.value);
Attempts:
2 left
💡 Hint
Check what happens to the prev pointer of the head node after insertions.
✗ Incorrect
After inserting 200 at head, dll.head.prev is null, so accessing .value causes TypeError.
🧠 Conceptual
advancedUnderstanding Node Connections in Doubly Linked List
In a doubly linked list, which statement about the 'prev' and 'next' pointers of the head and tail nodes is correct?
Attempts:
2 left
💡 Hint
Think about the start and end of the list and what nodes they connect to.
✗ Incorrect
Head has no previous node, so prev is null; tail has no next node, so next is null.
❓ Predict Output
expertOutput After Complex Insertions and Deletions
What is the printed state of the doubly linked list after these operations?
1. Insert 1 at end
2. Insert 2 at end
3. Insert 3 at head
4. Delete node with value 2
5. Insert 4 at end
6. Delete node with value 3
DSA Typescript
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; tail: Node | null = null; insertEnd(value: number) { 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; } insertHead(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } newNode.next = this.head; this.head.prev = newNode; this.head = newNode; } deleteValue(value: number) { let current = this.head; while (current) { if (current.value === value) { if (current.prev) { current.prev.next = current.next; } else { this.head = current.next; } if (current.next) { current.next.prev = current.prev; } else { this.tail = current.prev; } return; } current = current.next; } } 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.insertEnd(1); dll.insertEnd(2); dll.insertHead(3); dll.deleteValue(2); dll.insertEnd(4); dll.deleteValue(3); dll.printList();
Attempts:
2 left
💡 Hint
Track each operation carefully and update head and tail pointers.
✗ Incorrect
After all operations, nodes 3 and 2 are deleted, leaving 1 and 4 connected.
