Challenge - 5 Problems
Doubly Linked List Beginner
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting 10 at the beginning?
Consider a doubly linked list initially containing nodes with values 20 and 30. We insert 10 at the beginning. What is the printed 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; insertAtBeginning(value: number) { const newNode = new Node(value); if (!this.head) { this.head = this.tail = newNode; } else { newNode.next = this.head; this.head.prev = newNode; this.head = newNode; } } printList(): string { let current = this.head; let result = ''; while (current) { result += current.value + ' <-> '; current = current.next; } return result + 'null'; } } const dll = new DoublyLinkedList(); dll.insertAtBeginning(20); dll.insertAtBeginning(30); dll.insertAtBeginning(10); console.log(dll.printList());
Attempts:
2 left
💡 Hint
Remember that inserting at the beginning adds the new node before the current head.
✗ Incorrect
Inserting 10 at the beginning means 10 becomes the new head, followed by 30 and then 20. The code first inserts 20, then inserts 30 at the beginning (before 20), then 10 before 30.
❓ Predict Output
intermediateWhat is the list after inserting 5 at the beginning of an empty list?
Start with an empty doubly linked list. Insert 5 at the beginning. What is the printed 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; insertAtBeginning(value: number) { const newNode = new Node(value); if (!this.head) { this.head = this.tail = newNode; } else { newNode.next = this.head; this.head.prev = newNode; this.head = newNode; } } printList(): string { let current = this.head; let result = ''; while (current) { result += current.value + ' <-> '; current = current.next; } return result + 'null'; } } const dll = new DoublyLinkedList(); dll.insertAtBeginning(5); console.log(dll.printList());
Attempts:
2 left
💡 Hint
When the list is empty, the new node is both head and tail.
✗ Incorrect
Inserting 5 into an empty list creates a single node which is both head and tail. The print shows '5 <-> null'.
🔧 Debug
advancedWhy does this insertAtBeginning method cause a runtime error?
Identify the cause of the runtime error in this insertAtBeginning method for a doubly linked list.
DSA Typescript
insertAtBeginning(value: number) {
const newNode = new Node(value);
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
if (!this.tail) {
this.tail = newNode;
}
}Attempts:
2 left
💡 Hint
Check what happens when the list is empty and this.head is null.
✗ Incorrect
If the list is empty, this.head is null. Accessing this.head.prev causes a runtime error because you cannot access a property of null.
🧠 Conceptual
advancedWhat is the time complexity of inserting at the beginning of a doubly linked list?
Choose the correct time complexity for inserting a node at the beginning of a doubly linked list.
Attempts:
2 left
💡 Hint
Inserting at the beginning only changes a few pointers.
✗ Incorrect
Insertion at the beginning requires updating only a few pointers and does not depend on the list size, so it is O(1).
❓ Predict Output
expertWhat is the output after multiple insertions at the beginning?
Given the following code, what is the printed 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; insertAtBeginning(value: number) { const newNode = new Node(value); if (!this.head) { this.head = this.tail = newNode; } else { newNode.next = this.head; this.head.prev = newNode; this.head = newNode; } } printList(): string { let current = this.head; let result = ''; while (current) { result += current.value + ' <-> '; current = current.next; } return result + 'null'; } } const dll = new DoublyLinkedList(); dll.insertAtBeginning(1); dll.insertAtBeginning(2); dll.insertAtBeginning(3); dll.insertAtBeginning(4); dll.insertAtBeginning(5); console.log(dll.printList());
Attempts:
2 left
💡 Hint
Each insertion adds a new node before the current head.
✗ Incorrect
Inserting 5, then 4, then 3, then 2, then 1 at the beginning results in the list starting with 5 and ending with 1.
