Challenge - 5 Problems
Circular Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Circular Linked List Traversal
What is the printed output of the following JavaScript code that traverses a circular linked list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } // Create nodes const node1 = new Node(10); const node2 = new Node(20); const node3 = new Node(30); // Link nodes in a circle node1.next = node2; node2.next = node3; node3.next = node1; // Traverse circular linked list starting from node1 let current = node1; do { console.log(current.value); current = current.next; } while (current !== node1);
Attempts:
2 left
💡 Hint
Remember that traversal stops when we reach the starting node again.
✗ Incorrect
The traversal prints each node's value once, starting from node1. It stops when it reaches node1 again, so it prints 10, 20, 30 exactly once each.
🧠 Conceptual
intermediateUnderstanding Circular Linked List Traversal Condition
In a circular linked list traversal, why do we use a do-while loop with the condition `current !== head` instead of a while loop with `current !== null`?
Attempts:
2 left
💡 Hint
Think about what marks the end of traversal in a circular linked list.
✗ Incorrect
Circular linked lists loop back to the head node, so there is no null to mark the end. We stop when we reach the head again to avoid infinite loops.
❓ Predict Output
advancedOutput After Inserting Node in Circular Linked List
What is the printed output after inserting a new node with value 25 between nodes with values 20 and 30 in the circular linked list below?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } const node1 = new Node(10); const node2 = new Node(20); const node3 = new Node(30); node1.next = node2; node2.next = node3; node3.next = node1; // Insert new node with value 25 between node2 and node3 const node4 = new Node(25); node2.next = node4; node4.next = node3; // Traverse and print let current = node1; do { console.log(current.value); current = current.next; } while (current !== node1);
Attempts:
2 left
💡 Hint
Check the links after insertion carefully.
✗ Incorrect
The new node 25 is inserted after 20 and before 30, so traversal prints 10, 20, 25, 30.
🔧 Debug
advancedIdentify the Bug in Circular Linked List Traversal
What error will occur when running the following code to traverse a circular linked list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } const node1 = new Node(1); const node2 = new Node(2); const node3 = new Node(3); node1.next = node2; node2.next = node3; node3.next = node1; let current = node1; while (current !== node1) { console.log(current.value); current = current.next; }
Attempts:
2 left
💡 Hint
Check the loop condition and initial value of current.
✗ Incorrect
The while loop condition is false initially because current equals node1, so the loop body never runs and nothing is printed.
🚀 Application
expertCount Nodes in Circular Linked List
Given a circular linked list with unknown length, which code snippet correctly counts the number of nodes?
Attempts:
2 left
💡 Hint
Remember to count the head node and stop after full circle.
✗ Incorrect
Option B starts counting from head and uses do-while to include head node and stop after full circle. Others either miss head or use wrong condition.
