Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this code detecting a circular linked list?
Consider the following JavaScript code that checks if a linked list is circular. What will it print?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function isCircular(head) { if (!head) return false; let slow = head; let fast = head.next; while (fast && fast.next) { if (slow === fast) return true; slow = slow.next; fast = fast.next.next; } return false; } // Create nodes const a = new Node(1); const b = new Node(2); const c = new Node(3); const d = new Node(4); // Link nodes a.next = b; b.next = c; c.next = d; d.next = b; // Circular link here console.log(isCircular(a));
Attempts:
2 left
💡 Hint
Think about how the fast and slow pointers move and when they meet.
✗ Incorrect
The fast pointer moves two steps at a time, and the slow pointer moves one step. Because the list has a cycle (d.next = b), the fast pointer will eventually meet the slow pointer, so the function returns true.
❓ Predict Output
intermediateWhat is the output of this code for a non-circular linked list?
Look at this JavaScript code that checks if a linked list is circular. What will it print?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function isCircular(head) { if (!head) return false; let slow = head; let fast = head.next; while (fast && fast.next) { if (slow === fast) return true; slow = slow.next; fast = fast.next.next; } return false; } // Create nodes const a = new Node(1); const b = new Node(2); const c = new Node(3); const d = new Node(4); // Link nodes a.next = b; b.next = c; c.next = d; d.next = null; // No cycle console.log(isCircular(a));
Attempts:
2 left
💡 Hint
No node points back to an earlier node, so no cycle exists.
✗ Incorrect
Since the last node points to null, the fast pointer will reach the end of the list without meeting the slow pointer, so the function returns false.
🧠 Conceptual
advancedWhy does the fast pointer start at head.next in cycle detection?
In the cycle detection algorithm, why is the fast pointer initialized to head.next instead of head?
Attempts:
2 left
💡 Hint
Think about what happens if both pointers start at the same node.
✗ Incorrect
If both pointers start at the same node, the check slow === fast would be true immediately, incorrectly indicating a cycle. Starting fast at head.next avoids this.
🔧 Debug
advancedWhat error does this code raise when detecting a circular linked list?
This code tries to detect a cycle in a linked list. What error will it raise when run?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function isCircular(head) { let slow = head; let fast = head.next; while (fast && fast.next && fast.next.next) { if (slow === fast) return true; slow = slow.next; fast = fast.next.next; } return false; } const a = new Node(1); const b = new Node(2); const c = new Node(3); a.next = b; b.next = c; c.next = a; // cycle console.log(isCircular(a));
Attempts:
2 left
💡 Hint
Check the while loop condition and what happens when fast or fast.next is null.
✗ Incorrect
The while loop condition accesses fast.next and fast.next.next without checking if fast is null, causing a TypeError when fast becomes null.
🚀 Application
expertHow many nodes are in the circular linked list after these operations?
Given the following code that creates a circular linked list, how many unique nodes does the list contain?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } const head = new Node(1); const second = new Node(2); const third = new Node(3); const fourth = new Node(4); head.next = second; second.next = third; thrid.next = fourth; fourth.next = second; // cycle starts at second node // Count unique nodes in the cycle function countUniqueNodes(head) { let slow = head; let fast = head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; if (slow === fast) { let count = 1; let current = slow.next; while (current !== slow) { count++; current = current.next; } return count; } } return 0; } console.log(countUniqueNodes(head));
Attempts:
2 left
💡 Hint
The cycle starts at the second node, so count nodes from there until it loops back.
✗ Incorrect
The cycle includes nodes 2, 3, and 4. Counting these gives 3 unique nodes in the cycle.
