Challenge - 5 Problems
BFS Connected Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BFS Connected Components Count
What is the output of this Go code that counts connected components using BFS?
DSA Go
package main import "fmt" func bfs(graph map[int][]int, start int, visited map[int]bool) { queue := []int{start} visited[start] = true for len(queue) > 0 { node := queue[0] queue = queue[1:] for _, neighbor := range graph[node] { if !visited[neighbor] { visited[neighbor] = true queue = append(queue, neighbor) } } } } func countConnectedComponents(graph map[int][]int) int { visited := make(map[int]bool) count := 0 for node := range graph { if !visited[node] { bfs(graph, node, visited) count++ } } return count } func main() { graph := map[int][]int{ 1: {2}, 2: {1, 3}, 3: {2}, 4: {5}, 5: {4}, 6: {}, } fmt.Println(countConnectedComponents(graph)) }
Attempts:
2 left
💡 Hint
Count how many groups of nodes are connected without crossing to others.
✗ Incorrect
The graph has three connected components: {1,2,3}, {4,5}, and {6}. BFS visits all nodes in each component once, so the count is 3.
🔧 Debug
intermediateIdentify the error in BFS traversal code
What error will this Go code produce when running BFS on the graph?
DSA Go
package main import "fmt" func bfs(graph map[int][]int, start int, visited map[int]bool) { queue := []int{start} visited[start] = true for len(queue) > 0 { node := queue[0] queue = queue[1:] for _, neighbor := range graph[node] { if visited[neighbor] { visited[neighbor] = true queue = append(queue, neighbor) } } } } func main() { graph := map[int][]int{ 1: {1,2}, 2: {1, 3}, 3: {2}, } visited := make(map[int]bool) bfs(graph, 1, visited) fmt.Println(visited) }
Attempts:
2 left
💡 Hint
Check the condition that adds neighbors to the queue.
✗ Incorrect
The condition incorrectly checks if neighbor is already visited before adding it to the queue, causing repeated additions and an infinite loop.
🧠 Conceptual
advancedWhy use BFS for Connected Components?
Why is BFS a good choice to find connected components in an undirected graph?
Attempts:
2 left
💡 Hint
Think about how BFS visits nodes connected to the start node.
✗ Incorrect
BFS visits all nodes reachable from a start node before moving to another component, making it ideal to find connected components.
❓ Predict Output
advancedOutput after BFS visits nodes in a graph
What is the printed output of the visited map after running BFS starting from node 2?
DSA Go
package main import "fmt" func bfs(graph map[int][]int, start int, visited map[int]bool) { queue := []int{start} visited[start] = true for len(queue) > 0 { node := queue[0] queue = queue[1:] for _, neighbor := range graph[node] { if !visited[neighbor] { visited[neighbor] = true queue = append(queue, neighbor) } } } } func main() { graph := map[int][]int{ 1: {2, 3}, 2: {1, 4}, 3: {1}, 4: {2}, 5: {}, } visited := make(map[int]bool) for node := range graph { visited[node] = false } bfs(graph, 2, visited) fmt.Println(visited) }
Attempts:
2 left
💡 Hint
Check which nodes are reachable from node 2.
✗ Incorrect
Starting from 2, BFS visits 1 and 4, then from 1 it visits 3. Node 5 is isolated and remains unvisited.
🚀 Application
expertNumber of Connected Components in a Large Graph
Given a graph with nodes 1 to 7 and edges: (1-2), (2-3), (4-5), (5-6), (6-4), (7), how many connected components does BFS find?
Attempts:
2 left
💡 Hint
Group nodes by connectivity and count groups.
✗ Incorrect
The graph has three connected components: {1,2,3}, {4,5,6}, and {7} alone.
