Challenge - 5 Problems
Floyd 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 Java code that uses Floyd's algorithm to detect a cycle in a linked list?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } public class Test { public static boolean hasCycle(Node head) { Node slow = head; Node fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast) return true; } return false; } public static void main(String[] args) { 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 System.out.println(hasCycle(head)); } }
Attempts:
2 left
💡 Hint
Remember that Floyd's algorithm uses two pointers moving at different speeds to detect cycles.
✗ Incorrect
The code creates a linked list with a cycle by pointing the last node back to the second node. Floyd's algorithm detects this cycle by moving slow pointer one step and fast pointer two steps. When they meet, it returns true.
❓ Predict Output
intermediateOutput of Floyd's Algorithm on a Linked List without a Cycle
What will the following Java code print when Floyd's algorithm is used on a linked list without any cycle?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } public class Test { public static boolean hasCycle(Node head) { Node slow = head; Node fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast) return true; } return false; } public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); System.out.println(hasCycle(head)); } }
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 will reach null and the function returns false.
🔧 Debug
advancedIdentify the Error in Floyd's Cycle Detection Implementation
What error will the following Java code produce when trying to detect a cycle using Floyd's algorithm?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } public class Test { public static boolean hasCycle(Node head) { Node slow = head; Node fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast) return true; } return false; } public static void main(String[] args) { Node head = null; System.out.println(hasCycle(head)); } }
Attempts:
2 left
💡 Hint
Check the order of conditions in the while loop.
✗ Incorrect
The code checks fast.next before checking if fast is null, which can cause NullPointerException if fast is null.
🧠 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 what happens when two runners run on a circular track at different speeds.
✗ Incorrect
If there is a cycle, the fast pointer moves faster and will eventually meet the slow pointer inside the cycle, confirming its presence.
🚀 Application
expertFind the Starting Node of the Cycle Using Floyd's Algorithm
Given a linked list with a cycle, which code snippet correctly finds the node where the cycle begins using Floyd's algorithm?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } public class Test { public static Node detectCycleStart(Node head) { Node slow = head; Node fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast) { Node start = head; while (start != slow) { start = start.next; slow = slow.next; } return start; } } return null; } public static void main(String[] args) { 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 starts at node with data 2 Node cycleStart = detectCycleStart(head); System.out.println(cycleStart.data); } }
Attempts:
2 left
💡 Hint
After detecting the cycle, reset one pointer to head and move both one step at a time until they meet.
✗ Incorrect
After detecting the cycle, moving one pointer to head and advancing both pointers one step at a time leads them to meet at the cycle start node.
