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 circular linked list?
Given the linked list created and checked below, what will be printed?
DSA C#
public class Node { public int Value; public Node Next; public Node(int value) { Value = value; Next = null; } } public class LinkedList { public Node Head; public bool IsCircular() { if (Head == null) return false; Node slow = Head; Node fast = Head.Next; while (fast != null && fast.Next != null) { if (slow == fast) return true; slow = slow.Next; fast = fast.Next.Next; } return false; } } var list = new LinkedList(); list.Head = new Node(1); list.Head.Next = new Node(2); list.Head.Next.Next = new Node(3); list.Head.Next.Next.Next = list.Head; // Circular link System.Console.WriteLine(list.IsCircular());
Attempts:
2 left
💡 Hint
Think about how the fast and slow pointers move and meet in a circular list.
✗ Incorrect
The fast pointer moves two steps and slow moves one step. In a circular list, they will meet, so IsCircular returns true.
❓ Predict Output
intermediateWhat is the output when the linked list is not circular?
Consider the linked list below without any cycle. What will the IsCircular method print?
DSA C#
public class Node { public int Value; public Node Next; public Node(int value) { Value = value; Next = null; } } public class LinkedList { public Node Head; public bool IsCircular() { if (Head == null) return false; Node slow = Head; Node fast = Head.Next; while (fast != null && fast.Next != null) { if (slow == fast) return true; slow = slow.Next; fast = fast.Next.Next; } return false; } } var list = new LinkedList(); list.Head = new Node(1); list.Head.Next = new Node(2); list.Head.Next.Next = new Node(3); // No circular link System.Console.WriteLine(list.IsCircular());
Attempts:
2 left
💡 Hint
If the fast pointer reaches the end, the list is not circular.
✗ Incorrect
Since the list ends with null, fast pointer reaches null and the method returns false.
🔧 Debug
advancedWhy does this code cause an infinite loop when checking circularity?
Examine the code below. Why does it cause an infinite loop when the list is circular?
DSA C#
public class Node { public int Value; public Node Next; public Node(int value) { Value = value; Next = null; } } public class LinkedList { public Node Head; public bool IsCircular() { 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; } } var list = new LinkedList(); list.Head = new Node(1); list.Head.Next = new Node(2); list.Head.Next.Next = new Node(3); list.Head.Next.Next.Next = list.Head; // Circular System.Console.WriteLine(list.IsCircular());
Attempts:
2 left
💡 Hint
Check when the pointers are compared and when they move.
✗ Incorrect
Since slow and fast start at the same node and comparison happens after moving, they skip meeting at start and loop forever.
🧠 Conceptual
advancedWhat is the time complexity of detecting a circular linked list using Floyd's algorithm?
Consider Floyd's cycle detection algorithm for a linked list of length n. What is its time complexity?
Attempts:
2 left
💡 Hint
Think about how many times the pointers move through the list.
✗ Incorrect
Both pointers traverse the list at most a few times, so the time complexity is linear O(n).
🚀 Application
expertHow many nodes are in the cycle of this linked list?
Given the linked list below with a cycle, how many nodes are in the cycle?
DSA C#
public class Node { public int Value; public Node Next; public Node(int value) { Value = value; Next = null; } } public class LinkedList { public Node Head; public int CycleLength() { Node slow = Head; Node fast = Head; while (fast != null && fast.Next != null) { slow = slow.Next; fast = fast.Next.Next; if (slow == fast) { int length = 1; Node current = slow.Next; while (current != slow) { length++; current = current.Next; } return length; } } return 0; } } var list = new LinkedList(); list.Head = new Node(1); list.Head.Next = new Node(2); list.Head.Next.Next = new Node(3); list.Head.Next.Next.Next = new Node(4); list.Head.Next.Next.Next.Next = new Node(5); list.Head.Next.Next.Next.Next.Next = list.Head.Next.Next; // Cycle starts at node with value 3 System.Console.WriteLine(list.CycleLength());
Attempts:
2 left
💡 Hint
Count nodes starting from the meeting point until you reach it again.
✗ Incorrect
The cycle includes nodes with values 3, 4, and 5, so length is 3.
