Challenge - 5 Problems
BFS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BFS traversal on a simple graph
What is the output of the BFS traversal starting from node 0 in the given graph?
DSA Go
package main import ( "fmt" ) func bfs(graph map[int][]int, start int) []int { visited := make(map[int]bool) queue := []int{start} visited[start] = true order := []int{} for len(queue) > 0 { current := queue[0] queue = queue[1:] order = append(order, current) for _, neighbor := range graph[current] { if !visited[neighbor] { visited[neighbor] = true queue = append(queue, neighbor) } } } return order } func main() { graph := map[int][]int{ 0: {1, 2}, 1: {2}, 2: {0, 3}, 3: {3}, } result := bfs(graph, 0) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Remember BFS visits neighbors level by level, starting from the start node.
✗ Incorrect
BFS starts at node 0, visits neighbors 1 and 2 in order, then visits neighbors of 1 and 2. Node 3 is reached last.
🧠 Conceptual
intermediateBFS queue behavior on a graph with cycles
In BFS, why is it important to mark nodes as visited before adding them to the queue when the graph contains cycles?
Attempts:
2 left
💡 Hint
Think about what happens if a node is added multiple times in a cycle.
✗ Incorrect
Marking nodes as visited before enqueueing prevents repeated visits and infinite loops caused by cycles.
🔧 Debug
advancedIdentify the bug in BFS implementation
What error will occur when running this BFS code on a graph with nodes 0 to 3?
DSA Go
func bfsBug(graph map[int][]int, start int) []int { visited := make(map[int]bool) queue := []int{start} order := []int{} for len(queue) > 0 { current := queue[0] queue = queue[1:] order = append(order, current) for _, neighbor := range graph[current] { if !visited[neighbor] { queue = append(queue, neighbor) } } } return order }
Attempts:
2 left
💡 Hint
Check if nodes are marked visited anywhere.
✗ Incorrect
The code never marks nodes as visited, so nodes are added repeatedly causing an infinite loop.
📝 Syntax
advancedIdentify the syntax error in BFS code snippet
Which option contains the syntax error preventing BFS code from compiling?
DSA Go
func bfsSyntax(graph map[int][]int, start int) []int { visited := make(map[int]bool) queue := []int{start} visited[start] = true order := []int{} for len(queue) > 0 { current := queue[0] queue = queue[1:] order = append(order, current) for _, neighbor := range graph[current] { if !visited[neighbor] { visited[neighbor] = true queue = append(queue, neighbor) } } } return order }
Attempts:
2 left
💡 Hint
Check function parameter list syntax carefully.
✗ Incorrect
Option B is missing a comma between parameters, causing a syntax error.
🚀 Application
expertBFS shortest path length in unweighted graph
Given the BFS function below, what is the shortest path length from node 0 to node 4 in the graph? Return -1 if no path exists.
DSA Go
package main import ( "fmt" ) func bfsShortestPath(graph map[int][]int, start, target int) int { visited := make(map[int]bool) queue := []int{start} visited[start] = true distance := make(map[int]int) distance[start] = 0 for len(queue) > 0 { current := queue[0] queue = queue[1:] if current == target { return distance[current] } for _, neighbor := range graph[current] { if !visited[neighbor] { visited[neighbor] = true queue = append(queue, neighbor) distance[neighbor] = distance[current] + 1 } } } return -1 } func main() { graph := map[int][]int{ 0: {1, 2}, 1: {2, 3}, 2: {3}, 3: {4}, 4: {}, } result := bfsShortestPath(graph, 0, 4) fmt.Println(result) }
Attempts:
2 left
💡 Hint
BFS finds shortest paths in unweighted graphs by levels.
✗ Incorrect
The shortest path from 0 to 4 is 0 -> 1 -> 3 -> 4, which has length 3.
