Challenge - 5 Problems
Cycle Mastery Badge
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 where a cycle is created by connecting the last node back to the second node. What will the following C# code print?
DSA C#
public class Node { public int val; public Node next; public Node(int val) { this.val = val; this.next = null; } } public class LinkedListCycle { public static bool 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() { 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); System.Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
Remember Floyd's algorithm uses two pointers moving at different speeds to detect cycles.
✗ Incorrect
The fast pointer moves two steps and the slow pointer moves one step. If there is a cycle, they will eventually meet, so the function returns true.
❓ Predict Output
intermediateWhat is the output when no cycle exists in the linked list?
Given a linked list with nodes 1 -> 2 -> 3 -> 4 -> null (no cycle), what will the following code print?
DSA C#
public class Node { public int val; public Node next; public Node(int val) { this.val = val; this.next = null; } } public class LinkedListCycle { public static bool 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() { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); bool result = HasCycle(head); System.Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
If the fast pointer reaches the end, no cycle exists.
✗ Incorrect
Since the list ends with null and no cycle is formed, the fast pointer reaches null and the function returns false.
🔧 Debug
advancedWhat error does this code raise when detecting a cycle?
Analyze the following code snippet. What error will it raise when run?
DSA C#
public class Node { public int val; public Node next; public Node(int val) { this.val = val; this.next = null; } } public class LinkedListCycle { public static bool HasCycle(Node head) { Node slow = head; Node fast = head.next.next; // potential problem here while (fast != null && fast.next != null) { if (slow == fast) return true; slow = slow.next; fast = fast.next.next; } return false; } public static void Main() { Node head = new Node(1); bool result = HasCycle(head); System.Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
Check how fast pointer is initialized and if it can be null.
✗ Incorrect
The fast pointer is initialized as head.next.next without checking if head.next or head.next.next is null, causing NullReferenceException if the list is too short.
🧠 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 two pointers moving at different speeds behave in a cycle.
✗ Incorrect
If a cycle exists, the fast pointer moves faster and will eventually meet the slow pointer inside the cycle, confirming the cycle's presence.
🚀 Application
expertWhat is the output after removing the cycle from a linked list?
Given a linked list with a cycle created by connecting the last node back to the third node, the following code detects and removes the cycle. What will be the printed linked list after removal?
DSA C#
public class Node { public int val; public Node next; public Node(int val) { this.val = val; this.next = null; } } public class LinkedListCycle { public static bool DetectAndRemoveCycle(Node head) { Node slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast) { RemoveCycle(head, slow); return true; } } return false; } private static void RemoveCycle(Node head, Node meetingPoint) { Node ptr1 = head; Node ptr2 = meetingPoint; while (ptr1.next != ptr2.next) { ptr1 = ptr1.next; ptr2 = ptr2.next; } ptr2.next = null; } public static void PrintList(Node head) { Node current = head; while (current != null) { System.Console.Write(current.val + " -> "); current = current.next; } System.Console.WriteLine("null"); } public static void 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 = new Node(5); head.next.next.next.next.next = head.next.next; // cycle to node 3 bool hasCycle = DetectAndRemoveCycle(head); PrintList(head); } }
Attempts:
2 left
💡 Hint
After removal, the list should end with null and no cycles.
✗ Incorrect
The code detects the cycle and removes it by setting the last node's next to null. The printed list ends at 5 with null.
