Challenge - 5 Problems
Cycle Detection 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 cycle in a linked list?
Consider a linked list with nodes 1 -> 2 -> 3 -> 4 -> 5, where node 5 points back to node 3, creating a cycle. What will the function print?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function hasCycle(head) { let slow = head; let fast = head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; if (slow === fast) { return true; } } return false; } // Create nodes const head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5); // Create cycle: node 5 points to node 3 head.next.next.next.next.next = head.next.next; console.log(hasCycle(head));
Attempts:
2 left
💡 Hint
Think about what happens when fast and slow pointers meet in a cycle.
✗ Incorrect
The fast pointer moves two steps and the slow pointer moves one step. Because of the cycle, they will eventually meet, so the function returns true.
❓ Predict Output
intermediateWhat is the output when the linked list has no cycle?
Given a linked list 1 -> 2 -> 3 -> 4 -> 5 with no cycle, what will the function print?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function hasCycle(head) { let slow = head; let fast = head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; if (slow === fast) { return true; } } return false; } // Create nodes const head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5); console.log(hasCycle(head));
Attempts:
2 left
💡 Hint
No cycle means fast pointer will reach the end.
✗ Incorrect
Since there is no cycle, the fast pointer reaches the end (null) and the function returns false.
🧠 Conceptual
advancedWhy does Floyd's algorithm use two pointers moving at different speeds?
What is the main reason Floyd's cycle detection algorithm uses a slow pointer moving one step and a fast pointer moving two steps?
Attempts:
2 left
💡 Hint
Think about how the pointers behave inside a cycle.
✗ Incorrect
The fast pointer moves faster and will eventually catch up to the slow pointer inside the cycle, confirming a cycle exists.
❓ Predict Output
advancedWhat is the output of this code with a single node cycle?
A linked list has one node that points to itself, creating a cycle. What will the function print?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function hasCycle(head) { let slow = head; let fast = head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; if (slow === fast) { return true; } } return false; } const head = new Node(1); head.next = head; // cycle to itself console.log(hasCycle(head));
Attempts:
2 left
💡 Hint
A node pointing to itself forms a cycle.
✗ Incorrect
Since the node points to itself, the slow and fast pointers meet immediately, so the function returns true.
🔧 Debug
expertWhat error does this code raise?
This code attempts to detect a cycle but has a bug. What error will it raise when run?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function hasCycle(head) { let slow = head; let fast = head; while (fast && fast.next && fast.next.next) { slow = slow.next; fast = fast.next.next; if (slow === fast) { return true; } } return false; } const head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); console.log(hasCycle(head));
Attempts:
2 left
💡 Hint
Check the while loop condition for null safety.
✗ Incorrect
The code does not check if fast is null before accessing fast.next, so if fast is null, accessing fast.next causes a TypeError.
