Bird
Raised Fist0
DSA Rustprogramming~10 mins

Detect Cycle in Linked List Floyd's Algorithm in DSA Rust - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the slow pointer to the head of the linked list.

DSA Rust
let mut slow = [1];
Drag options to blanks, or click blank then click option'
Ahead
BNone
Ctail
Dfast
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing slow to None or tail instead of head.
2fill in blank
medium

Complete the code to move the fast pointer two steps ahead safely.

DSA Rust
[1]
Drag options to blanks, or click blank then click option'
Afast = fast.next.next;
Bfast = fast.unwrap().next.unwrap().next;
Cfast = fast.next;
Dfast = fast.and_then(|node| node.next.as_ref()).and_then(|node| node.next.as_ref()).map(|node| &**node);
Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap which can cause panic if next is None.
3fill in blank
hard

Fix the error in the loop condition to check if fast and fast.next are not None.

DSA Rust
while fast.is_some() && [1] {
Drag options to blanks, or click blank then click option'
Afast.is_none()
Bfast.next.is_some()
Cfast.unwrap().next.is_some()
Dfast.next.is_none()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access fast.next directly without unwrap.
4fill in blank
hard

Fill both blanks to move slow by one step and fast by two steps safely.

DSA Rust
slow = [1];
fast = [2];
Drag options to blanks, or click blank then click option'
Aslow.and_then(|node| node.next.as_ref()).map(|node| &**node)
Bslow.next
Cfast.next
Dfast.and_then(|node| node.next.as_ref()).and_then(|node| node.next.as_ref()).map(|node| &**node)
Attempts:
3 left
💡 Hint
Common Mistakes
Using direct next without Option handling.
5fill in blank
hard

Fill all three blanks to return true if slow equals fast, else false after loop.

DSA Rust
if slow == [1] {
    return [2];
}

return [3];
Drag options to blanks, or click blank then click option'
Afast
Btrue
Cfalse
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Returning true or false incorrectly.

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 faster (fast) and one slower (slow) through the list.
  2. Step 2: Detect cycle by pointer meeting

    If the fast pointer meets the slow pointer, a cycle exists; otherwise, no cycle.
  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 at different speeds detect cycle fast [OK]
Common Mistakes:
  • Thinking counting nodes detects cycle
  • Assuming reversing list helps detect cycle
  • Using extra memory instead of pointers
2. Which Rust code snippet correctly initializes the slow and fast pointers for Floyd's cycle detection in a linked list named head?
easy
A. let mut slow = head.as_ref(); let mut fast = head.as_ref();
B. let mut slow = head.clone(); let mut fast = head.clone();
C. let mut slow = &head.next; let mut fast = &head.next.next;
D. let mut slow = &head; let mut fast = &head.next;

Solution

  1. Step 1: Initialize both pointers at head

    Floyd's algorithm starts both slow and fast pointers at the head node.
  2. Step 2: Correct Rust reference usage

    Using head.as_ref() for both slow and fast is correct; fast moves faster later.
  3. Final Answer:

    let mut slow = head.as_ref(); let mut fast = head.as_ref(); -> Option A
  4. Quick Check:

    Both pointers start at head [OK]
Hint: Start both pointers at head node [OK]
Common Mistakes:
  • Starting fast pointer at head.next before loop
  • Cloning nodes unnecessarily
  • Using wrong references causing borrow errors
3. What will be the output of the following Rust code snippet detecting a cycle in a linked list?
struct Node { val: i32, next: Option }

fn has_cycle(head: &Option) -> bool {
    let mut slow = head.as_ref();
    let mut fast = head.as_ref();
    while fast.is_some() && fast.unwrap().next.is_some() {
        slow = slow.unwrap().next.as_ref();
        fast = fast.unwrap().next.as_ref().unwrap().next.as_ref();
        if slow.map(|n| n as *const ()) == fast.map(|n| n as *const ()) {
            return true;
        }
    }
    false
}

// List: 1 -> 2 -> 3 -> 4 -> 2 (cycle back to node with value 2)
medium
A. Infinite loop
B. false
C. true
D. Compilation error

Solution

  1. Step 1: Understand the list structure

    The list has a cycle starting at node with value 2, so cycle exists.
  2. Step 2: Trace Floyd's pointers

    Slow and fast pointers will eventually meet inside the cycle, returning true.
  3. Final Answer:

    true -> Option C
  4. Quick Check:

    Cycle present = true [OK]
Hint: Cycle in list means pointers meet = true [OK]
Common Mistakes:
  • Assuming no cycle returns true
  • Confusing Option with references
  • Missing unwrap() causing panic
4. Identify the bug in this Rust code snippet for detecting a cycle using Floyd's algorithm:
fn has_cycle(head: &Option) -> bool {
    let mut slow = head.as_ref();
    let mut fast = head.as_ref();
    while fast.is_some() && fast.unwrap().next.is_some() {
        slow = slow.unwrap().next.as_ref();
        fast = fast.unwrap().next.as_ref(); // Bug here
        if slow.map(|n| n as *const ()) == fast.map(|n| n as *const ()) {
            return true;
        }
    }
    false
}
medium
A. slow pointer moves two steps instead of one
B. fast pointer moves only one step instead of two
C. Missing check for slow being None
D. Incorrect comparison of slow and fast pointers

Solution

  1. Step 1: Analyze fast pointer movement

    Fast pointer should move two steps each loop, but here it moves only one step.
  2. Step 2: Understand impact on cycle detection

    Moving fast only one step breaks Floyd's logic, so cycle detection fails or slows.
  3. Final Answer:

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

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

Solution

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

    First, detect if cycle exists by moving slow and fast pointers.
  2. Step 2: Find cycle start node

    Reset slow to head, then move slow and fast one step each 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 D
  4. Quick Check:

    Reset slow and move both pointers to find cycle start [OK]
Hint: Reset slow to head after cycle detection to find start [OK]
Common Mistakes:
  • Trying to count nodes instead of pointer moves
  • Using extra memory instead of pointer reset
  • Reversing list which breaks cycle detection
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