Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Detect Cycle in Linked List Floyd's Algorithm in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Floyd's Cycle Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Floyd's Cycle Detection on a Linked List with a Cycle
What is the output of the following code that uses Floyd's algorithm to detect a cycle in a linked list?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def has_cycle(head):
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False

# Create linked list: 1 -> 2 -> 3 -> 4 -> 2 (cycle back to node with value 2)
head = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
head.next = node2
node2.next = node3
node3.next = node4
node4.next = node2

print(has_cycle(head))
ARuntimeError
BTrue
CNone
DFalse
Attempts:
2 left
💡 Hint
Remember that Floyd's algorithm returns True if slow and fast pointers meet inside the list.
❓ Predict Output
intermediate
2:00remaining
Output of Floyd's Cycle Detection on a Linked List without a Cycle
What is the output of the following code that uses Floyd's algorithm to detect a cycle in a linked list without a cycle?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def has_cycle(head):
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False

# Create linked list: 1 -> 2 -> 3 -> 4 -> None
head = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
head.next = node2
node2.next = node3
node3.next = node4
node4.next = None

print(has_cycle(head))
ATrue
BIndexError
CNone
DFalse
Attempts:
2 left
💡 Hint
If the fast pointer reaches the end, there is no cycle.
🧠 Conceptual
advanced
1:30remaining
Why 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?
ABecause both pointers move at the same speed to compare nodes
BBecause the slow pointer moves faster to find the cycle quickly
CBecause the fast pointer will eventually catch up to the slow pointer if there is a cycle
DBecause the fast pointer skips nodes to reduce memory usage
Attempts:
2 left
💡 Hint
Think about what happens when two runners run around a circular track at different speeds.
🔧 Debug
advanced
2:00remaining
Identify the error in this Floyd's cycle detection implementation
What error will this code produce when trying to detect a cycle in a linked list?
DSA Python
def has_cycle(head):
    slow = head
    fast = head
    while fast.next and fast.next.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False
AAttributeError
BTypeError
CNo error, works correctly
DNameError
Attempts:
2 left
💡 Hint
Check what happens if head is None.
🚀 Application
expert
2:30remaining
Number of nodes in cycle after detection using Floyd's Algorithm
After detecting a cycle using Floyd's algorithm, how can you find the number of nodes in the cycle?
DSA Python
def count_cycle_nodes(meeting_point):
    count = 1
    current = meeting_point.next
    while current != meeting_point:
        current = current.next
        count += 1
    return count

# Assume meeting_point is the node where slow and fast pointers met inside the cycle
AStart from meeting_point and move until you return to it, counting nodes
BCount nodes from head until meeting_point
CCount nodes from head until None
DCount nodes from meeting_point to head
Attempts:
2 left
💡 Hint
The cycle length is the number of nodes in the loop starting and ending at the meeting point.

Practice

(1/5)
1. What is the main idea behind Floyd's Cycle Detection Algorithm in a linked list?
easy
A. Use two pointers moving at different speeds to detect a cycle
B. Count the number of nodes and check if it exceeds a limit
C. Reverse the linked list and check if it matches the original
D. Use a hash set to store visited nodes and check for repeats

Solution

  1. Step 1: Understand Floyd's algorithm concept

    It uses two pointers, one moving fast (two steps) and one slow (one step).
  2. Step 2: Detect cycle by pointer meeting

    If the fast pointer meets the slow pointer, a cycle exists in the linked list.
  3. Final Answer:

    Use two pointers moving at different speeds to detect a cycle -> Option A
  4. Quick Check:

    Two pointers at different speeds = cycle detection [OK]
Hint: Two pointers, slow and fast, meet if cycle exists [OK]
Common Mistakes:
  • Thinking counting nodes detects cycle
  • Assuming reversing list helps detect cycle
  • Confusing hash set method with Floyd's algorithm
2. Which of the following is the correct way to move the fast pointer in Floyd's cycle detection?
easy
A. fast = fast.next
B. fast = fast.next.next
C. fast = fast.next.next.next
D. fast = fast.next.prev

Solution

  1. Step 1: Understand fast pointer movement

    The fast pointer moves two steps at a time to catch up with the slow pointer.
  2. Step 2: Identify correct syntax

    Moving two nodes ahead is done by fast = fast.next.next.
  3. Final Answer:

    fast = fast.next.next -> Option B
  4. Quick Check:

    Fast pointer moves two steps = fast.next.next [OK]
Hint: Fast pointer moves two nodes ahead using next.next [OK]
Common Mistakes:
  • Moving fast pointer only one step
  • Trying to move fast pointer three steps
  • Using invalid pointer like prev in singly linked list
3. What will be the output of the following code snippet?
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = head.next  # cycle here

def has_cycle(head):
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False

print(has_cycle(head))
medium
A. True
B. None
C. False
D. Error

Solution

  1. Step 1: Analyze linked list structure

    Nodes 1->2->3 and then 3 points back to 2, creating a cycle.
  2. Step 2: Trace Floyd's algorithm

    Slow and fast pointers will meet inside the cycle, so function returns True.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    Cycle exists, so output = True [OK]
Hint: Cycle means slow and fast pointers meet, so return True [OK]
Common Mistakes:
  • Ignoring cycle creation line
  • Assuming no cycle returns True
  • Confusing None return with False
4. Find the error in the following Floyd's cycle detection code:
def detect_cycle(head):
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next
        if slow == fast:
            return True
    return False
medium
A. No return statement for cycle detection
B. slow pointer moves two steps instead of one
C. Missing initialization of slow pointer
D. fast pointer moves only one step instead of two

Solution

  1. Step 1: Check pointer movements

    Fast pointer should move two steps (fast = fast.next.next), but here it moves one step.
  2. Step 2: Understand impact on cycle detection

    Moving one step slows fast pointer, so it may never meet slow pointer correctly.
  3. Final Answer:

    fast pointer moves only one step instead of two -> Option D
  4. Quick Check:

    Fast pointer must move two steps [OK]
Hint: Fast pointer must jump two nodes, not one [OK]
Common Mistakes:
  • Moving fast pointer one step
  • Confusing slow pointer movement
  • Forgetting to check fast and fast.next
5. Given a linked list where the last node points back to the head (forming a cycle), how can Floyd's algorithm be adapted to find the node where the cycle begins?
hard
A. Use a hash set to store nodes and return the first repeated node
B. Count nodes until slow and fast meet, then return count as cycle start
C. After detecting cycle, reset slow to head and move both pointers one step until they meet
D. Reverse the linked list and check where reversal breaks

Solution

  1. Step 1: Detect cycle using Floyd's algorithm

    Use slow and fast pointers to find meeting point inside cycle.
  2. Step 2: Find cycle start node

    Reset slow to head, then move slow and fast one step at a time until they meet; meeting point is cycle start.
  3. Final Answer:

    After detecting cycle, reset slow to head and move both pointers one step until they meet -> Option C
  4. Quick Check:

    Reset slow and move both pointers one step to find cycle start [OK]
Hint: Reset slow to head, move both one step to find cycle start [OK]
Common Mistakes:
  • Trying to count nodes to find cycle start
  • Using reversal which doesn't help here
  • Confusing hash set method with Floyd's approach
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard