Challenge - 5 Problems
Tail Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting nodes at the tail
What is the printed linked list after inserting 10, 20, and 30 at the tail in order?
DSA Typescript
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class LinkedList { head: Node | null = null; tail: Node | null = null; insertAtTail(value: number): void { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail!.next = newNode; this.tail = newNode; } } printList(): string { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const list = new LinkedList(); list.insertAtTail(10); list.insertAtTail(20); list.insertAtTail(30); console.log(list.printList());
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end and how the list is printed from head to tail.
✗ Incorrect
Nodes are inserted at the tail, so the order is preserved as 10, then 20, then 30. The printList method traverses from head to tail, printing each node's value followed by '->', ending with 'null'.
❓ Predict Output
intermediateLinked list state after multiple tail insertions
What is the linked list printed after inserting 5, 15, 25, and 35 at the tail?
DSA Typescript
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class LinkedList { head: Node | null = null; tail: Node | null = null; insertAtTail(value: number): void { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail!.next = newNode; this.tail = newNode; } } printList(): string { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const list = new LinkedList(); list.insertAtTail(5); list.insertAtTail(15); list.insertAtTail(25); list.insertAtTail(35); console.log(list.printList());
Attempts:
2 left
💡 Hint
Remember that tail insertion adds nodes at the end, preserving order.
✗ Incorrect
Each new node is added at the tail, so the list grows as 5, then 15, then 25, then 35. The printList method prints from head to tail.
🔧 Debug
advancedIdentify the error in tail insertion
What error occurs when running this code that tries to insert nodes at the tail?
DSA Typescript
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class LinkedList { head: Node | null = null; tail: Node | null = null; insertAtTail(value: number): void { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail!.next = newNode; } // Missing tail update here } printList(): string { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const list = new LinkedList(); list.insertAtTail(1); list.insertAtTail(2); list.insertAtTail(3); console.log(list.printList());
Attempts:
2 left
💡 Hint
Check if the tail pointer is updated after insertion.
✗ Incorrect
The tail pointer is never updated after the first insertion, so tail remains the first node. New nodes are linked but tail.next is not updated properly, causing the list to end prematurely when printing.
❓ Predict Output
advancedLinked list after tail insertions and one head insertion
What is the printed linked list after inserting 100 at head, then 200 and 300 at tail?
DSA Typescript
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class LinkedList { head: Node | null = null; tail: Node | null = null; insertAtHead(value: number): void { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { newNode.next = this.head; this.head = newNode; } } insertAtTail(value: number): void { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail!.next = newNode; this.tail = newNode; } } printList(): string { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const list = new LinkedList(); list.insertAtHead(100); list.insertAtTail(200); list.insertAtTail(300); console.log(list.printList());
Attempts:
2 left
💡 Hint
Head insertion adds at front, tail insertion adds at end.
✗ Incorrect
First 100 is inserted at head, so list is 100. Then 200 and 300 are inserted at tail, so list becomes 100 -> 200 -> 300 -> null.
🧠 Conceptual
expertWhy is tail pointer important in tail insertion?
Which statement best explains why maintaining a tail pointer is important when inserting nodes at the end of a singly linked list?
Attempts:
2 left
💡 Hint
Think about how you find the end of the list without a tail pointer.
✗ Incorrect
Maintaining a tail pointer allows direct access to the last node, so insertion at the end is done in constant time. Without it, you must traverse from head to tail each time, which is slower.
