Challenge - 5 Problems
Cycle Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Floyd's Cycle Detection on a Linked List with a Cycle
What is the output of the following C++ code that uses Floyd's algorithm to detect a cycle in a linked list?
DSA C++
#include <iostream> struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; bool hasCycle(Node* head) { Node* slow = head; Node* fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; if (slow == fast) return true; } return false; } int main() { Node* 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 = head->next; // cycle here bool result = hasCycle(head); if (result) std::cout << "Cycle detected" << std::endl; else std::cout << "No cycle" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that Floyd's algorithm uses two pointers moving at different speeds to detect cycles.
✗ Incorrect
The linked list has a cycle because the last node points back to the second node. Floyd's algorithm detects this by the slow and fast pointers meeting inside the cycle, so the output is 'Cycle detected'.
❓ Predict Output
intermediateOutput of Floyd's Algorithm on a Linked List without a Cycle
What will the following C++ code print when Floyd's cycle detection algorithm is run on a linked list without any cycle?
DSA C++
#include <iostream> struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; bool hasCycle(Node* head) { Node* slow = head; Node* fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; if (slow == fast) return true; } return false; } int main() { Node* head = new Node(10); head->next = new Node(20); head->next->next = new Node(30); head->next->next->next = new Node(40); bool result = hasCycle(head); if (result) std::cout << "Cycle detected" << std::endl; else std::cout << "No cycle" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Think about what happens when the fast pointer reaches the end of the list.
✗ Incorrect
Since the linked list has no cycle, the fast pointer reaches the end (nullptr) and the function returns false, so the output is 'No cycle'.
🔧 Debug
advancedIdentify the Bug in Floyd's Cycle Detection Implementation
The following code is intended to detect a cycle in a linked list using Floyd's algorithm. What is the bug that causes it to fail or crash?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
bool hasCycle(Node* head) {
Node* slow = head;
Node* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}Attempts:
2 left
💡 Hint
Check the order of pointer checks in the while loop condition to avoid null pointer dereference.
✗ Incorrect
The condition fast->next assumes fast is not nullptr, but fast can be nullptr causing a crash. The correct condition is to check fast != nullptr before fast->next.
🧠 Conceptual
advancedWhy Does Floyd's Algorithm Use Two Pointers Moving at Different Speeds?
Why does Floyd's cycle detection algorithm use two pointers moving at different speeds (slow and fast) to detect a cycle in a linked list?
Attempts:
2 left
💡 Hint
Think about how two runners on a circular track eventually meet if one runs faster.
✗ Incorrect
The fast pointer moves twice as fast as the slow pointer. If a cycle exists, the fast pointer will lap the slow pointer inside the cycle, causing them to meet.
🚀 Application
expertFind the Starting Node of the Cycle Using Floyd's Algorithm
After detecting a cycle in a linked list using Floyd's algorithm, how can you find the node where the cycle begins?
Attempts:
2 left
💡 Hint
After detecting the cycle, resetting one pointer to head and moving both at the same speed helps locate the start.
✗ Incorrect
Once slow and fast meet inside the cycle, resetting slow to head and moving both pointers one step at a time will cause them to meet at the cycle's starting node.
