Challenge - 5 Problems
Floyd's Cycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Floyd's Cycle Detection on a Linked List with a Cycle
What is the output of the following Go code that uses Floyd's algorithm to detect a cycle in a linked list?
DSA Go
package main import "fmt" type Node struct { val int next *Node } func hasCycle(head *Node) bool { slow, fast := head, head for fast != nil && fast.next != nil { slow = slow.next fast = fast.next.next if slow == fast { return true } } return false } func main() { n1 := &Node{val: 1} n2 := &Node{val: 2} n3 := &Node{val: 3} n4 := &Node{val: 4} n1.next = n2 n2.next = n3 n3.next = n4 n4.next = n2 // cycle here fmt.Println(hasCycle(n1)) }
Attempts:
2 left
💡 Hint
Remember that Floyd's algorithm uses two pointers moving at different speeds to detect cycles.
✗ Incorrect
The code creates a linked list with a cycle starting at node with value 2. Floyd's algorithm detects this cycle by the slow and fast pointers meeting, so the output is true.
❓ Predict Output
intermediateOutput of Floyd's Algorithm on a Linked List without a Cycle
What will the following Go code print when checking a linked list with no cycle using Floyd's algorithm?
DSA Go
package main import "fmt" type Node struct { val int next *Node } func hasCycle(head *Node) bool { slow, fast := head, head for fast != nil && fast.next != nil { slow = slow.next fast = fast.next.next if slow == fast { return true } } return false } func main() { n1 := &Node{val: 1} n2 := &Node{val: 2} n3 := &Node{val: 3} n4 := &Node{val: 4} n1.next = n2 n2.next = n3 n3.next = n4 n4.next = nil // no cycle fmt.Println(hasCycle(n1)) }
Attempts:
2 left
💡 Hint
If the fast pointer reaches the end of the list, there is no cycle.
✗ Incorrect
Since the list ends with nil and no cycle is formed, the function returns false.
🔧 Debug
advancedIdentify the Error in Floyd's Cycle Detection Implementation
What error will the following Go code produce when trying to detect a cycle in a linked list?
DSA Go
package main import "fmt" type Node struct { val int next *Node } func hasCycle(head *Node) bool { slow, fast := head, head for fast.next != nil && fast.next.next != nil { slow = slow.next fast = fast.next.next if slow == fast { return true } } return false } func main() { fmt.Println(hasCycle(nil)) }
Attempts:
2 left
💡 Hint
Check the order of conditions in the for loop to avoid accessing nil pointers.
✗ Incorrect
The code checks fast.next before ensuring fast is not nil, causing a nil pointer dereference panic.
🧠 Conceptual
advancedWhy Does Floyd's Algorithm Use Two Pointers Moving at Different Speeds?
Why does Floyd's cycle detection algorithm use a slow pointer moving one step and a fast pointer moving two steps?
Attempts:
2 left
💡 Hint
Think about how two pointers moving at different speeds behave in a cycle.
✗ Incorrect
The fast pointer moves faster and will eventually meet the slow pointer inside the cycle, confirming its presence.
🚀 Application
expertFind the Starting Node of the Cycle Using Floyd's Algorithm
Given a linked list with a cycle, which code snippet correctly finds the node where the cycle begins after detecting the cycle using Floyd's algorithm?
DSA Go
func detectCycleStart(head *Node) *Node {
slow, fast := head, head
for fast != nil && fast.next != nil {
slow = slow.next
fast = fast.next.next
if slow == fast {
// Find start of cycle
slow = head
for slow != fast {
slow = slow.next
fast = fast.next
}
return slow
}
}
return nil
}Attempts:
2 left
💡 Hint
After detecting the cycle, resetting slow to head and moving both pointers one step at a time finds the cycle start.
✗ Incorrect
Resetting slow to head and moving both pointers one step at a time makes them meet at the cycle start node.
