Challenge - 5 Problems
Traversal Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Forward Traversal in a Doubly Linked List
What is the printed output of the following JavaScript code that traverses a doubly linked list forward?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } traverseForward() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new DoublyLinkedList(); list.append(10); list.append(20); list.append(30); list.traverseForward();
Attempts:
2 left
💡 Hint
Think about how the traversal moves from head to tail using the next pointer.
✗ Incorrect
The traverseForward method starts at the head and moves through each node using the next pointer, printing values in order: 10, 20, 30, then null.
❓ Predict Output
intermediateOutput of Backward Traversal in a Doubly Linked List
What is the printed output of the following JavaScript code that traverses a doubly linked list backward?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } traverseBackward() { let current = this.tail; let result = ''; while (current) { result += current.value + ' -> '; current = current.prev; } result += 'null'; console.log(result); } } const list = new DoublyLinkedList(); list.append(5); list.append(15); list.append(25); list.traverseBackward();
Attempts:
2 left
💡 Hint
Backward traversal starts from the tail and moves using the prev pointer.
✗ Incorrect
The traverseBackward method starts at the tail and moves backward through the list using the prev pointer, printing values in reverse order: 25, 15, 5, then null.
🧠 Conceptual
advancedUnderstanding Traversal Pointers in Doubly Linked Lists
In a doubly linked list, which pointer is used to move backward during traversal?
Attempts:
2 left
💡 Hint
Backward means going to the previous node.
✗ Incorrect
The prev pointer in each node points to the previous node, allowing backward traversal.
🔧 Debug
advancedIdentify the Bug in Backward Traversal Code
What error will occur when running the following backward traversal code snippet?
DSA Javascript
traverseBackward() {
let current = this.head;
let result = '';
while (current) {
result += current.value + ' -> ';
current = current.prev;
}
result += 'null';
console.log(result);
}Attempts:
2 left
💡 Hint
Check where traversal starts and which pointer is used.
✗ Incorrect
Starting backward traversal from head and moving with prev pointer causes an infinite loop or no progress because head.prev is null and loop condition never ends properly.
🚀 Application
expertResulting List After Mixed Forward and Backward Traversals
Given a doubly linked list with nodes 1, 2, 3 appended in order, what is the printed output after the following operations?
1. Traverse forward and print values.
2. Traverse backward and print values.
3. Append 4.
4. Traverse backward and print values again.
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } traverseForward() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } traverseBackward() { let current = this.tail; let result = ''; while (current) { result += current.value + ' -> '; current = current.prev; } result += 'null'; console.log(result); } } const list = new DoublyLinkedList(); list.append(1); list.append(2); list.append(3); list.traverseForward(); list.traverseBackward(); list.append(4); list.traverseBackward();
Attempts:
2 left
💡 Hint
Remember that append adds to the tail and backward traversal starts from tail.
✗ Incorrect
First forward traversal prints 1 -> 2 -> 3 -> null. Backward traversal prints 3 -> 2 -> 1 -> null. After appending 4, backward traversal prints 4 -> 3 -> 2 -> 1 -> null.
