Challenge - 5 Problems
Circular Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of traversing a circular linked list with 3 nodes
What is the printed output of the following Go code that traverses a circular linked list with 3 nodes?
DSA Go
package main import "fmt" type Node struct { data int next *Node } func main() { n1 := &Node{data: 1} n2 := &Node{data: 2} n3 := &Node{data: 3} n1.next = n2 n2.next = n3 n3.next = n1 // circular link current := n1 count := 0 for { fmt.Print(current.data) if count < 2 { fmt.Print(" -> ") } current = current.next count++ if current == n1 { break } } fmt.Print(" -> null") }
Attempts:
2 left
💡 Hint
Remember that traversal stops when the current node reaches the start node again.
✗ Incorrect
The code prints nodes 1, 2, and 3 once each, then stops because it detects the circular link back to the start node n1. So the output is '1 -> 2 -> 3 -> null'.
🧠 Conceptual
intermediateWhy use a do-while style loop for circular linked list traversal?
In Go, which reason best explains why a do-while style loop (executing at least once before checking condition) is preferred for traversing a circular linked list?
Attempts:
2 left
💡 Hint
Think about how to detect when you have visited all nodes exactly once in a circle.
✗ Incorrect
In a circular linked list, there is no nil to mark the end. We must process the first node before checking if we returned to it, so a do-while style loop ensures the first node is processed.
🔧 Debug
advancedIdentify the bug causing infinite loop in circular linked list traversal
What is the bug in this Go code that causes an infinite loop when traversing a circular linked list?
DSA Go
package main import "fmt" type Node struct { data int next *Node } func main() { n1 := &Node{data: 1} n2 := &Node{data: 2} n3 := &Node{data: 3} n1.next = n2 n2.next = n3 n3.next = n1 current := n1 for current != nil { fmt.Print(current.data, " -> ") current = current.next } fmt.Print("null") }
Attempts:
2 left
💡 Hint
Think about what happens when you check for nil in a circular linked list.
✗ Incorrect
Since the list is circular, current never becomes nil, so the loop never ends, causing an infinite loop.
❓ Predict Output
advancedOutput after inserting a node in a circular linked list
What is the printed output after inserting a new node with data 4 after node with data 2 in this circular linked list traversal?
DSA Go
package main import "fmt" type Node struct { data int next *Node } func main() { n1 := &Node{data: 1} n2 := &Node{data: 2} n3 := &Node{data: 3} n1.next = n2 n2.next = n3 n3.next = n1 // Insert new node with data 4 after n2 n4 := &Node{data: 4} n4.next = n2.next n2.next = n4 current := n1 for { fmt.Print(current.data) if current.next != n1 { fmt.Print(" -> ") } current = current.next if current == n1 { break } } fmt.Print(" -> null") }
Attempts:
2 left
💡 Hint
Check the order of nodes after insertion carefully.
✗ Incorrect
The new node 4 is inserted after node 2, so the order is 1 -> 2 -> 4 -> 3 -> null.
🧠 Conceptual
expertDetecting loop in a singly linked list using Floyd's cycle-finding algorithm
Which statement best describes how Floyd's cycle-finding algorithm detects a loop in a singly linked list?
Attempts:
2 left
💡 Hint
Think about how two pointers moving at different speeds can detect cycles.
✗ Incorrect
Floyd's algorithm uses a slow pointer moving one step and a fast pointer moving two steps; if they meet, the list has a cycle.
