Bird
0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 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.