Challenge - 5 Problems
Tail Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting nodes at the tail?
Consider a singly linked list initially empty. We insert nodes with values 5, 10, and 15 at the tail in that order. What is the printed state of the list after these insertions?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtTail(data) { 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; } printList() { let result = ''; let current = this.head; while (current) { result += current.data + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.insertAtTail(5); list.insertAtTail(10); list.insertAtTail(15); list.printList();
Attempts:
2 left
💡 Hint
Think about how inserting at the tail adds nodes to the end of the list.
✗ Incorrect
Inserting at the tail means each new node is added after the last node. So the list grows as 5, then 5->10, then 5->10->15.
🧠 Conceptual
intermediateWhy is tail insertion efficient in a linked list with a tail pointer?
If a linked list maintains a pointer to the tail node, what is the main advantage of inserting a new node at the tail?
Attempts:
2 left
💡 Hint
Think about how having direct access to the last node helps.
✗ Incorrect
With a tail pointer, you can add a new node directly after the tail without walking through the list, making insertion constant time.
🔧 Debug
advancedIdentify the bug in this tail insertion code
What error will this code cause when inserting nodes at the tail?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtTail(data) { 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; } }
Attempts:
2 left
💡 Hint
Check what happens when the list is empty and a node is inserted.
✗ Incorrect
When the list is empty, the code sets head to newNode but then continues to traverse and adds newNode again at the end, causing duplicate insertion.
📝 Syntax
advancedWhat error does this tail insertion code produce?
What error will this code raise when trying to insert at tail?
DSA Javascript
insertAtTail(data) {
const newNode = new Node(data)
if (!this.head) {
this.head = newNode
} else {
let current = this.head
while (current.next) {
current = current.next
}
current.next = newNode
}
return
}Attempts:
2 left
💡 Hint
JavaScript does not require semicolons strictly; check variable definitions.
✗ Incorrect
The code is syntactically correct and will run without errors assuming Node class is defined.
🚀 Application
expertAfter multiple tail insertions, what is the length of the list?
A linked list is initially empty. We insert nodes with values 1 through 7 at the tail one by one. What is the length of the list after all insertions?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtTail(data) { 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; } getLength() { let count = 0; let current = this.head; while (current) { count++; current = current.next; } return count; } } const list = new LinkedList(); for (let i = 1; i <= 7; i++) { list.insertAtTail(i); } console.log(list.getLength());
Attempts:
2 left
💡 Hint
Count how many nodes were inserted.
✗ Incorrect
Seven nodes were inserted, so the length is 7.
