Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
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 Cyclic List
What is the output of the following C code that uses Floyd's algorithm to detect a cycle in a linked list?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

int detectCycle(Node* head) {
    Node *slow = head, *fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) return 1;
    }
    return 0;
}

int main() {
    Node* head = malloc(sizeof(Node));
    Node* second = malloc(sizeof(Node));
    Node* third = malloc(sizeof(Node));
    head->data = 1; head->next = second;
    second->data = 2; second->next = third;
    third->data = 3; third->next = second; // cycle here

    printf("%d\n", detectCycle(head));
    return 0;
}
A1
B0
CSegmentation fault
DCompilation error
Attempts:
2 left
💡 Hint
Remember that Floyd's algorithm returns 1 if a cycle is detected.
❓ Predict Output
intermediate
2:00remaining
Output of Floyd's Cycle Detection on a Non-Cyclic List
What is the output of the following C code that uses Floyd's algorithm to detect a cycle in a linked list without a cycle?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

int detectCycle(Node* head) {
    Node *slow = head, *fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) return 1;
    }
    return 0;
}

int main() {
    Node* head = malloc(sizeof(Node));
    Node* second = malloc(sizeof(Node));
    Node* third = malloc(sizeof(Node));
    head->data = 1; head->next = second;
    second->data = 2; second->next = third;
    third->data = 3; third->next = NULL; // no cycle

    printf("%d\n", detectCycle(head));
    return 0;
}
A1
BCompilation error
CSegmentation fault
D0
Attempts:
2 left
💡 Hint
If there is no cycle, the function returns 0.
🔧 Debug
advanced
2:00remaining
Identify the Error in Cycle Detection Code
What error will this code produce when trying to detect a cycle in a linked list?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

int detectCycle(Node* head) {
    Node *slow = head, *fast = head;
    while (fast->next && fast->next->next) {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) return 1;
    }
    return 0;
}
ACompilation error due to pointer misuse
BSegmentation fault if head is NULL
CInfinite loop if list has no cycle
DNo error, works correctly
Attempts:
2 left
💡 Hint
Check if the code handles empty list (head == NULL) safely.
🧠 Conceptual
advanced
2:00remaining
Why Floyd's Algorithm Uses Two Pointers Moving at Different Speeds?
Why does Floyd's cycle detection algorithm use two pointers moving at different speeds (slow and fast)?
ATo guarantee that if a cycle exists, the fast pointer will eventually meet the slow pointer inside the cycle
BTo ensure the fast pointer reaches the end faster for no cycle detection
CTo reduce memory usage by avoiding extra data structures
DTo sort the linked list while detecting cycles
Attempts:
2 left
💡 Hint
Think about how two pointers moving at different speeds behave in a cycle.
🚀 Application
expert
3:00remaining
Find the Starting Node of the Cycle Using Floyd's Algorithm
Given a linked list with a cycle, after detecting the cycle using Floyd's algorithm, how do you find the node where the cycle begins?
AMove fast pointer two steps and slow pointer one step until fast reaches NULL
BReverse the linked list and check for repeated nodes
CReset slow pointer to head, then move slow and fast one step at a time until they meet; the meeting point is the cycle start
DCount the number of nodes in the list and divide by two
Attempts:
2 left
💡 Hint
After detecting the cycle, resetting slow to head helps locate the cycle start.

Practice

(1/5)
1. What is the main purpose of Floyd's Cycle Detection Algorithm in a linked list?
easy
A. To find the middle element of the linked list
B. To detect if there is a cycle (loop) in the linked list
C. To reverse the linked list
D. To sort the linked list in ascending order

Solution

  1. Step 1: Understand Floyd's Algorithm goal

    Floyd's Cycle Detection Algorithm is designed to find if a linked list has a cycle or loop.
  2. Step 2: Compare with other linked list operations

    Sorting, reversing, or finding the middle element are different operations and not related to cycle detection.
  3. Final Answer:

    To detect if there is a cycle (loop) in the linked list -> Option B
  4. Quick Check:

    Cycle detection = Detect cycle [OK]
Hint: Floyd's algorithm is famous for cycle detection [OK]
Common Mistakes:
  • Confusing cycle detection with sorting
  • Thinking it finds middle element
  • Assuming it reverses the list
2. Which of the following is the correct way to initialize the two pointers used in Floyd's Cycle Detection Algorithm in C?
easy
A. Node *slow = head; Node *fast = head->next;
B. Node *slow = NULL; Node *fast = head;
C. Node *slow = head->next; Node *fast = head->next->next;
D. Node *slow = head; Node *fast = head;

Solution

  1. Step 1: Understand pointer initialization in Floyd's algorithm

    Both slow and fast pointers start at the head node to begin traversal.
  2. Step 2: Check options for correct initialization

    Node *slow = head; Node *fast = head; initializes both pointers at head, which is the standard approach.
  3. Final Answer:

    Node *slow = head; Node *fast = head; -> Option D
  4. Quick Check:

    Both pointers start at head [OK]
Hint: Both pointers start at head node in Floyd's algorithm [OK]
Common Mistakes:
  • Starting fast pointer ahead of slow incorrectly
  • Initializing slow as NULL
  • Skipping initialization of one pointer
3. What will be the output of the following C code snippet using Floyd's algorithm on a linked list with a cycle?
typedef struct Node {
  int data;
  struct Node *next;
} Node;

int detectCycle(Node *head) {
  Node *slow = head, *fast = head;
  while (fast && fast->next) {
    slow = slow->next;
    fast = fast->next->next;
    if (slow == fast) return 1;
  }
  return 0;
}

// Assume linked list has a cycle
printf("%d", detectCycle(head));
medium
A. 1
B. 0
C. Compilation error
D. Infinite loop

Solution

  1. Step 1: Analyze the detectCycle function logic

    The function moves slow pointer by one and fast pointer by two nodes. If they meet, it returns 1 indicating a cycle.
  2. Step 2: Given the list has a cycle, pointers will meet

    Since the list has a cycle, slow and fast pointers will eventually meet, so function returns 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Cycle present = return 1 [OK]
Hint: Cycle means slow and fast pointers meet, return 1 [OK]
Common Mistakes:
  • Assuming function returns 0 for cycle
  • Thinking code causes infinite loop
  • Confusing return values
4. Identify the bug in this Floyd's cycle detection code snippet:
int detectCycle(Node *head) {
  Node *slow = head, *fast = head;
  while (fast != NULL && fast->next != NULL) {
    slow = slow->next;
    fast = fast->next;
    if (slow == fast) return 1;
    fast = fast->next;
  }
  return 0;
}
medium
A. fast pointer moves two steps but not checked for NULL before second move
B. slow pointer moves two steps instead of one
C. fast pointer moves only one step instead of two
D. slow pointer is not initialized properly

Solution

  1. Step 1: Analyze pointer movements in the loop

    Slow moves one step, fast moves one step, then checks if slow == fast, before the second fast move.
  2. Step 2: Problem with check timing

    In Floyd's algorithm, fast should move two steps before the cycle check. Here, the check occurs after fast has moved only one step, and the second fast move is done without checking if fast->next is NULL, which can cause a NULL pointer dereference.
  3. Final Answer:

    fast pointer moves two steps but not checked for NULL before second move -> Option A
  4. Quick Check:

    Second fast move lacks NULL check [OK]
Hint: Fast pointer must complete two steps with NULL checks before cycle check [OK]
Common Mistakes:
  • Checking cycle after fast's first step only
  • Confusing slow and fast pointer steps
  • Not initializing pointers correctly
5. You have a linked list where a cycle exists. After detecting the cycle using Floyd's algorithm, how can you find the starting node of the cycle?
hard
A. Move fast pointer two steps and slow pointer one step until they meet again
B. Count the number of nodes in the list and divide by two
C. Reset slow pointer to head, then move slow and fast one step at a time until they meet
D. Reverse the linked list and check for cycle again

Solution

  1. Step 1: Detect cycle and pointers meet inside the loop

    Floyd's algorithm detects cycle when slow and fast pointers meet inside the loop.
  2. Step 2: Reset slow to head and move both pointers one step at a time

    Reset slow to head, then move slow and fast one step each until they meet again; this meeting point is the cycle start.
  3. Final Answer:

    Reset slow pointer to head, then move slow and fast one step at a time until they meet -> Option C
  4. Quick Check:

    Reset slow + move both one step = cycle start [OK]
Hint: Reset slow to head, move both pointers stepwise to find cycle start [OK]
Common Mistakes:
  • Moving fast pointer two steps again
  • Trying to count nodes to find cycle start
  • Reversing list to detect cycle start
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