Challenge - 5 Problems
Circular List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Go code detecting circular linked list?
Consider the following Go code that checks if a linked list is circular. What will it print?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func isCircular(head *Node) bool { if head == nil { return false } slow, fast := head, head.next for fast != nil && fast.next != nil { if slow == fast { return true } slow = slow.next fast = fast.next.next } return false } func main() { n1 := &Node{value: 1} n2 := &Node{value: 2} n3 := &Node{value: 3} n4 := &Node{value: 4} n1.next = n2 n2.next = n3 n3.next = n4 n4.next = n2 // Creates a cycle if isCircular(n1) { fmt.Println("Circular") } else { fmt.Println("Not Circular") } }
Attempts:
2 left
💡 Hint
Think about how the fast and slow pointers move and meet in a cycle.
✗ Incorrect
The fast pointer moves two steps and the slow pointer moves one step. Because there is a cycle, they will eventually meet, so the function returns true and prints "Circular".
❓ Predict Output
intermediateWhat does this Go code print when the linked list has no cycle?
Analyze the following Go code that checks if a linked list is circular. The list has no cycle. What will it print?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func isCircular(head *Node) bool { if head == nil { return false } slow, fast := head, head.next for fast != nil && fast.next != nil { if slow == fast { return true } slow = slow.next fast = fast.next.next } return false } func main() { n1 := &Node{value: 1} n2 := &Node{value: 2} n3 := &Node{value: 3} n4 := &Node{value: 4} n1.next = n2 n2.next = n3 n3.next = n4 n4.next = nil // No cycle if isCircular(n1) { fmt.Println("Circular") } else { fmt.Println("Not Circular") } }
Attempts:
2 left
💡 Hint
No node points back to a previous node, so no cycle exists.
✗ Incorrect
Since n4.next is nil, the list ends normally. The fast pointer reaches nil and the function returns false, printing "Not Circular".
🧠 Conceptual
advancedWhy does Floyd's cycle detection algorithm use two pointers moving at different speeds?
In detecting if a linked list is circular, why does Floyd's algorithm use a slow pointer moving one step and a fast pointer moving two steps?
Attempts:
2 left
💡 Hint
Think about how two runners on a track can meet if one runs faster.
✗ Incorrect
The fast pointer moves faster and will lap the slow pointer inside the cycle, guaranteeing detection if a cycle exists.
❓ Predict Output
advancedWhat error does this Go code produce when checking a nil linked list?
What happens when the following Go code runs with a nil linked list (head is nil)?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func isCircular(head *Node) bool { slow, fast := head, head.next for fast != nil && fast.next != nil { if slow == fast { return true } slow = slow.next fast = fast.next.next } return false } func main() { var head *Node = nil if isCircular(head) { fmt.Println("Circular") } else { fmt.Println("Not Circular") } }
Attempts:
2 left
💡 Hint
Check what happens when head is nil and you try to access head.next.
✗ Incorrect
The code tries to access head.next without checking if head is nil, causing a runtime panic due to nil pointer dereference.
🚀 Application
expertHow many nodes are in the linked list after this code runs?
Given the following Go code that creates a linked list with a cycle, how many unique nodes can be traversed starting from head before repeating?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func main() { n1 := &Node{value: 1} n2 := &Node{value: 2} n3 := &Node{value: 3} n4 := &Node{value: 4} n5 := &Node{value: 5} n1.next = n2 n2.next = n3 n3.next = n4 n4.next = n5 n5.next = n3 // cycle starts at n3 count := 0 current := n1 visited := make(map[*Node]bool) for current != nil { if visited[current] { break } visited[current] = true count++ current = current.next } fmt.Println(count) }
Attempts:
2 left
💡 Hint
Count unique nodes before the cycle repeats.
✗ Incorrect
The cycle starts at node with value 3. Nodes 1,2,3,4,5 are unique before revisiting node 3 again. So count is 5.
