Challenge - 5 Problems
Cycle Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of cycle detection on a simple graph
What is the output of the following Go code that detects a cycle in an undirected graph?
DSA Go
package main import "fmt" type Graph struct { adj map[int][]int } func (g *Graph) addEdge(u, v int) { g.adj[u] = append(g.adj[u], v) g.adj[v] = append(g.adj[v], u) } func (g *Graph) dfs(v, parent int, visited map[int]bool) bool { visited[v] = true for _, neighbor := range g.adj[v] { if !visited[neighbor] { if g.dfs(neighbor, v, visited) { return true } } else if neighbor != parent { return true } } return false } func (g *Graph) hasCycle() bool { visited := make(map[int]bool) for vertex := range g.adj { if !visited[vertex] { if g.dfs(vertex, -1, visited) { return true } } } return false } func main() { g := Graph{adj: make(map[int][]int)} g.addEdge(1, 2) g.addEdge(2, 3) g.addEdge(3, 4) g.addEdge(4, 2) fmt.Println(g.hasCycle()) }
Attempts:
2 left
💡 Hint
Look for back edges in DFS that are not the parent node.
✗ Incorrect
The graph contains a cycle formed by edges 2-3-4-2. The DFS detects this cycle and returns true.
❓ Predict Output
intermediateCycle detection output on a tree graph
What will the output be when running this Go code that checks for cycles in an undirected graph structured as a tree?
DSA Go
package main import "fmt" type Graph struct { adj map[int][]int } func (g *Graph) addEdge(u, v int) { g.adj[u] = append(g.adj[u], v) g.adj[v] = append(g.adj[v], u) } func (g *Graph) dfs(v, parent int, visited map[int]bool) bool { visited[v] = true for _, neighbor := range g.adj[v] { if !visited[neighbor] { if g.dfs(neighbor, v, visited) { return true } } else if neighbor != parent { return true } } return false } func (g *Graph) hasCycle() bool { visited := make(map[int]bool) for vertex := range g.adj { if !visited[vertex] { if g.dfs(vertex, -1, visited) { return true } } } return false } func main() { g := Graph{adj: make(map[int][]int)} g.addEdge(1, 2) g.addEdge(1, 3) g.addEdge(3, 4) g.addEdge(3, 5) fmt.Println(g.hasCycle()) }
Attempts:
2 left
💡 Hint
A tree has no cycles.
✗ Incorrect
The graph is a tree structure with no cycles, so the function returns false.
🔧 Debug
advancedIdentify the error causing incorrect cycle detection
The following Go code is intended to detect cycles in an undirected graph but incorrectly returns true even when no cycle exists. What is the bug?
DSA Go
package main import "fmt" type Graph struct { adj map[int][]int } func (g *Graph) addEdge(u, v int) { g.adj[u] = append(g.adj[u], v) g.adj[v] = append(g.adj[v], u) } func (g *Graph) dfs(v int, visited map[int]bool) bool { visited[v] = true for _, neighbor := range g.adj[v] { if !visited[neighbor] { if g.dfs(neighbor, visited) { return true } } else { return true } } return false } func (g *Graph) hasCycle() bool { visited := make(map[int]bool) for vertex := range g.adj { if !visited[vertex] { if g.dfs(vertex, visited) { return true } } } return false } func main() { g := Graph{adj: make(map[int][]int)} g.addEdge(1, 2) g.addEdge(2, 3) g.addEdge(3, 4) fmt.Println(g.hasCycle()) }
Attempts:
2 left
💡 Hint
In undirected graphs, you must ignore the edge back to the parent node during DFS.
✗ Incorrect
The DFS function does not track the parent node, so it treats the immediate parent as a cycle, causing incorrect results.
🧠 Conceptual
advancedUnderstanding cycle detection logic
In cycle detection for an undirected graph using DFS, why do we need to check if a visited neighbor is not the parent node?
Attempts:
2 left
💡 Hint
Think about the edge you just traveled to reach the current node.
✗ Incorrect
When DFS visits neighbors, the edge back to the parent is not a cycle but a normal traversal edge, so it must be ignored.
❓ Predict Output
expertOutput of cycle detection on disconnected graph with multiple components
What is the output of the following Go code that detects cycles in an undirected graph with two disconnected components, one with a cycle and one without?
DSA Go
package main import "fmt" type Graph struct { adj map[int][]int } func (g *Graph) addEdge(u, v int) { g.adj[u] = append(g.adj[u], v) g.adj[v] = append(g.adj[v], u) } func (g *Graph) dfs(v, parent int, visited map[int]bool) bool { visited[v] = true for _, neighbor := range g.adj[v] { if !visited[neighbor] { if g.dfs(neighbor, v, visited) { return true } } else if neighbor != parent { return true } } return false } func (g *Graph) hasCycle() bool { visited := make(map[int]bool) for vertex := range g.adj { if !visited[vertex] { if g.dfs(vertex, -1, visited) { return true } } } return false } func main() { g := Graph{adj: make(map[int][]int)} // Component 1 with cycle g.addEdge(1, 2) g.addEdge(2, 3) g.addEdge(3, 1) // Component 2 without cycle g.addEdge(4, 5) g.addEdge(5, 6) fmt.Println(g.hasCycle()) }
Attempts:
2 left
💡 Hint
The function checks all components for cycles.
✗ Incorrect
The first component has a cycle, so the function returns true even though the second component does not have one.
