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 on this graph?
DSA Rust
use std::collections::VecDeque;
fn bfs_shortest_path(graph: &Vec<Vec<usize>>, start: usize) -> Vec<Option<usize>> {
let mut dist = vec![None; graph.len()];
let mut queue = VecDeque::new();
dist[start] = Some(0);
queue.push_back(start);
while let Some(u) = queue.pop_front() {
for &v in &graph[u] {
if dist[v].is_none() {
dist[v] = Some(dist[u].unwrap() + 1);
queue.push_back(v);
}
}
}
dist
}
fn main() {
let graph = vec![
vec![1, 2], // 0
vec![0, 3], // 1
vec![0, 3, 4], // 2
vec![1, 2, 5], // 3
vec![2], // 4
vec![3], // 5
];
let dist = bfs_shortest_path(&graph, 0);
println!("{:?}", dist);
}Attempts:
2 left
💡 Hint
Trace BFS levels starting from node 0. Nodes directly connected to 0 have distance 1.
✗ Incorrect
Starting from node 0 with distance 0, nodes 1 and 2 are at distance 1. Node 3 is connected to 1 and 2, so distance 2. Node 4 is connected to 2, distance 2. Node 5 is connected to 3, distance 3.
🧠 Conceptual
intermediateWhy BFS Finds Shortest Path in Unweighted Graph
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 level by level, so the first time it reaches a node, it is via the shortest path from the start node.
🔧 Debug
advancedIdentify the Bug in BFS Implementation
What is the bug in this BFS shortest path code snippet?
DSA Rust
fn bfs_bug(graph: &Vec<Vec<usize>>, start: usize) -> Vec<Option<usize>> {
let mut dist = vec![None; graph.len()];
let mut queue = std::collections::VecDeque::new();
dist[start] = Some(0);
queue.push_back(start);
while let Some(u) = queue.pop_front() {
for &v in &graph[u] {
dist[v] = Some(dist[u].unwrap() + 1);
queue.push_back(v);
}
}
dist
}Attempts:
2 left
💡 Hint
Check if the code prevents revisiting nodes.
✗ Incorrect
The code assigns distance to neighbors without checking if they were visited before, so nodes can be revisited multiple times, causing incorrect distances or infinite loops.
🚀 Application
advancedShortest Path Reconstruction Using BFS
Which code snippet correctly reconstructs the shortest path from start to target after BFS?
DSA Rust
Assume BFS fills a parent array where parent[v] = u means u is predecessor of v in shortest path.
Attempts:
2 left
💡 Hint
Trace path from target back to start using parent, then reverse.
✗ Incorrect
Option C correctly pushes nodes from target to start, then reverses to get path from start to target. It unwraps Option safely. Option C goes forward incorrectly. Option C misses pushing start. Option C misses unwrap causing compile error.
❓ Predict Output
expertOutput of BFS Distances with Disconnected Nodes
What is the output of the distance vector after running BFS from node 0 on this graph with disconnected nodes?
DSA Rust
use std::collections::VecDeque;
fn bfs(graph: &Vec<Vec<usize>>, start: usize) -> Vec<Option<usize>> {
let mut dist = vec![None; graph.len()];
let mut queue = VecDeque::new();
dist[start] = Some(0);
queue.push_back(start);
while let Some(u) = queue.pop_front() {
for &v in &graph[u] {
if dist[v].is_none() {
dist[v] = Some(dist[u].unwrap() + 1);
queue.push_back(v);
}
}
}
dist
}
fn main() {
let graph = vec![
vec![1], // 0
vec![0, 2], // 1
vec![1], // 2
vec![4], // 3 disconnected component
vec![3], // 4
];
let dist = bfs(&graph, 0);
println!("{:?}", dist);
}Attempts:
2 left
💡 Hint
Nodes 3 and 4 are disconnected from start node 0.
✗ Incorrect
BFS only visits nodes reachable from start. Nodes 3 and 4 are disconnected, so their distances remain None.
