Challenge - 5 Problems
Shortest Path BFS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of BFS shortest path distances
What is the output of the following TypeScript code that finds shortest path distances from node 0 in an unweighted graph?
DSA Typescript
function bfsShortestPath(graph: number[][], start: number): number[] {
const distances = Array(graph.length).fill(-1);
const queue: number[] = [];
distances[start] = 0;
queue.push(start);
while (queue.length > 0) {
const node = queue.shift()!;
for (const neighbor of graph[node]) {
if (distances[neighbor] === -1) {
distances[neighbor] = distances[node] + 1;
queue.push(neighbor);
}
}
}
return distances;
}
const graph = [
[1, 2],
[0, 3],
[0, 3],
[1, 2, 4],
[3]
];
console.log(bfsShortestPath(graph, 0));Attempts:
2 left
💡 Hint
Think about how BFS explores neighbors level by level starting from the start node.
✗ Incorrect
The BFS starts at node 0 with distance 0. Nodes 1 and 2 are directly connected to 0, so distance 1. Node 3 is connected to 1 and 2, so distance 2. Node 4 is connected to 3, so distance 3.
🧠 Conceptual
intermediate1:30remaining
Understanding BFS queue behavior
In BFS for shortest path in an unweighted graph, what is the main reason for using a queue data structure?
Attempts:
2 left
💡 Hint
Think about how BFS explores nodes level by level.
✗ Incorrect
A queue ensures nodes are processed in the order they are discovered, which corresponds to increasing distance from the start node. This guarantees shortest path discovery in unweighted graphs.
🔧 Debug
advanced2:00remaining
Identify the bug in BFS shortest path code
What error will this TypeScript BFS shortest path code produce when run, and why?
DSA Typescript
function bfsShortestPath(graph: number[][], start: number): number[] {
const distances = Array(graph.length).fill(-1);
const queue: number[] = [];
distances[start] = 0;
queue.push(start);
while (queue.length > 0) {
const node = queue.pop()!;
for (const neighbor of graph[node]) {
if (distances[neighbor] === -1) {
distances[neighbor] = distances[node] + 1;
queue.push(neighbor);
}
}
}
return distances;
}Attempts:
2 left
💡 Hint
Check how the queue is used to remove elements.
✗ Incorrect
Using pop() removes the last element, making the traversal depth-first instead of breadth-first, so distances are incorrect for shortest path.
🚀 Application
advanced2:30remaining
Shortest path reconstruction from BFS distances
Given BFS distances from start node 0, how can you reconstruct the actual shortest path to node 4 in the graph below?
Graph adjacency list:
0: [1, 2]
1: [0, 3]
2: [0, 3]
3: [1, 2, 4]
4: [3]
Distances from 0: [0, 1, 1, 2, 3]
Attempts:
2 left
💡 Hint
Shortest path can be traced backward using distances.
✗ Incorrect
By moving from the target node to neighbors with distance one less, you trace the shortest path back to the start node.
🧠 Conceptual
expert1:30remaining
Why BFS guarantees shortest path in unweighted graphs
Why does BFS always find the shortest path in an unweighted graph?
Attempts:
2 left
💡 Hint
Think about the order nodes are visited in BFS.
✗ Incorrect
BFS visits nodes in layers based on their distance from the start node, so the first time a node is visited is via the shortest path.