Challenge - 5 Problems
Shortest Path BFS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BFS Shortest Path Distance Array
What is the output of the distance array after running BFS from node 0 in the given graph?
DSA Java
import java.util.*; public class Graph { static int V = 5; static ArrayList<ArrayList<Integer>> adj = new ArrayList<>(); public static void addEdge(int u, int v) { adj.get(u).add(v); adj.get(v).add(u); } public static int[] bfsShortestPath(int src) { int[] dist = new int[V]; Arrays.fill(dist, -1); Queue<Integer> q = new LinkedList<>(); dist[src] = 0; q.add(src); while (!q.isEmpty()) { int u = q.poll(); for (int v : adj.get(u)) { if (dist[v] == -1) { dist[v] = dist[u] + 1; q.add(v); } } } return dist; } public static void main(String[] args) { for (int i = 0; i < V; i++) adj.add(new ArrayList<>()); addEdge(0, 1); addEdge(0, 2); addEdge(1, 3); addEdge(2, 3); addEdge(3, 4); int[] dist = bfsShortestPath(0); System.out.println(Arrays.toString(dist)); } }
Attempts:
2 left
💡 Hint
Think about the shortest number of edges from node 0 to each node.
✗ Incorrect
BFS explores neighbors level by level. From node 0, nodes 1 and 2 are at distance 1, node 3 is reached via 1 or 2 at distance 2, and node 4 is reached from 3 at distance 3.
🧠 Conceptual
intermediateWhy BFS Finds Shortest Path in Unweighted Graphs
Why does BFS guarantee the shortest path in an unweighted graph?
Attempts:
2 left
💡 Hint
Think about the order in which BFS visits nodes.
✗ Incorrect
BFS visits nodes layer by layer, so the first time it reaches a node is via the shortest path from the source.
🔧 Debug
advancedIdentify the Bug in BFS Distance Calculation
What error will occur when running this BFS code snippet for shortest path distances?
DSA Java
int[] dist = new int[V]; Arrays.fill(dist, 0); Queue<Integer> q = new LinkedList<>(); dist[src] = 0; q.add(src); while (!q.isEmpty()) { int u = q.poll(); for (int v : adj.get(u)) { if (dist[v] == 0) { dist[v] = dist[u] + 1; q.add(v); } } }
Attempts:
2 left
💡 Hint
Consider how unvisited nodes are identified.
✗ Incorrect
Using 0 to mark unvisited nodes conflicts with the source node distance 0, so other nodes with distance 0 are never visited.
🚀 Application
advancedFind Shortest Path Length Between Two Nodes
Given an unweighted graph and two nodes, which code snippet correctly returns the shortest path length between them using BFS?
Attempts:
2 left
💡 Hint
Shortest path is found when target is first reached in BFS.
✗ Incorrect
In BFS, the first time the target node is visited, the shortest path length is known and can be returned immediately.
❓ Predict Output
expertOutput of BFS Path Reconstruction
What is the output path from node 0 to node 4 after running BFS with parent tracking?
DSA Java
import java.util.*; public class GraphPath { static int V = 5; static ArrayList<ArrayList<Integer>> adj = new ArrayList<>(); public static void addEdge(int u, int v) { adj.get(u).add(v); adj.get(v).add(u); } public static List<Integer> bfsPath(int src, int dest) { int[] parent = new int[V]; Arrays.fill(parent, -1); Queue<Integer> q = new LinkedList<>(); boolean[] visited = new boolean[V]; visited[src] = true; q.add(src); while (!q.isEmpty()) { int u = q.poll(); if (u == dest) break; for (int v : adj.get(u)) { if (!visited[v]) { visited[v] = true; parent[v] = u; q.add(v); } } } List<Integer> path = new ArrayList<>(); if (parent[dest] == -1 && src != dest) return path; for (int v = dest; v != -1; v = parent[v]) { path.add(v); } Collections.reverse(path); return path; } public static void main(String[] args) { for (int i = 0; i < V; i++) adj.add(new ArrayList<>()); addEdge(0, 1); addEdge(0, 2); addEdge(1, 3); addEdge(2, 3); addEdge(3, 4); List<Integer> path = bfsPath(0, 4); System.out.println(path); } }
Attempts:
2 left
💡 Hint
BFS finds shortest path; parent array tracks the path backwards.
✗ Incorrect
BFS visits node 1 before node 2 at the first level, so the path through 1 is recorded first and reconstructed as [0,1,3,4].
