Challenge - 5 Problems
Bellman Ford Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bellman-Ford on a graph with negative edge weights
What is the output distances array after running Bellman-Ford on this graph from source vertex 0?
DSA Rust
let edges = vec![
(0, 1, 4),
(0, 2, 5),
(1, 2, -3),
(2, 3, 4),
(3, 1, 6),
];
let vertices = 4;
let source = 0;
let mut dist = vec![i32::MAX; vertices];
dist[source] = 0;
for _ in 0..vertices - 1 {
for &(u, v, w) in &edges {
if dist[u] != i32::MAX && dist[u] + w < dist[v] {
dist[v] = dist[u] + w;
}
}
}
println!("{:?}", dist);Attempts:
2 left
💡 Hint
Remember Bellman-Ford relaxes edges multiple times to find shortest paths even with negative weights.
✗ Incorrect
The algorithm updates distances step-by-step. Initially, dist[0]=0. Relaxing edges updates dist[1]=4, then dist[2]=1 (via 1->2 with weight -3), and dist[3]=5 (via 2->3 with weight 4).
🧠 Conceptual
intermediateDetecting negative weight cycles with Bellman-Ford
Which condition correctly detects a negative weight cycle after running Bellman-Ford?
Attempts:
2 left
💡 Hint
Bellman-Ford runs |V|-1 times to relax edges; extra relaxation means cycle.
✗ Incorrect
After |V|-1 iterations, if any edge can still reduce a distance, it means a cycle with negative total weight exists.
🔧 Debug
advancedIdentify the bug in this Bellman-Ford implementation snippet
What is the bug in this Rust code snippet for Bellman-Ford?
DSA Rust
for _ in 0..vertices { for &(u, v, w) in &edges { if dist[u] + w < dist[v] { dist[v] = dist[u] + w; } } }
Attempts:
2 left
💡 Hint
Check what happens when dist[u] is i32::MAX and you add w.
✗ Incorrect
Adding w to i32::MAX causes integer overflow, leading to incorrect comparisons and wrong results.
🚀 Application
advancedUsing Bellman-Ford to find shortest paths in a graph with negative edges
Given a graph with edges [(0,1,1), (1,2,-1), (2,3,-1), (3,1,-1)], what does Bellman-Ford detect when run from vertex 0?
Attempts:
2 left
💡 Hint
Look for cycles where the sum of edge weights is negative.
✗ Incorrect
The cycle 1->2->3->1 has total weight -3, which is negative and reachable from source 0, so Bellman-Ford detects it.
❓ Predict Output
expertFinal distance array after Bellman-Ford with multiple negative edges but no negative cycle
What is the final distance array after running Bellman-Ford from source 0 on this graph?
Edges: [(0,1,2), (1,2,-5), (2,3,1), (3,4,2), (4,1,2)]
Vertices: 5
DSA Rust
let edges = vec![
(0, 1, 2),
(1, 2, -5),
(2, 3, 1),
(3, 4, 2),
(4, 1, 2),
];
let vertices = 5;
let source = 0;
let mut dist = vec![i32::MAX; vertices];
dist[source] = 0;
for _ in 0..vertices - 1 {
for &(u, v, w) in &edges {
if dist[u] != i32::MAX && dist[u] + w < dist[v] {
dist[v] = dist[u] + w;
}
}
}
println!("{:?}", dist);Attempts:
2 left
💡 Hint
Relax edges multiple times and track distance updates carefully.
✗ Incorrect
Distances update as: dist[1]=2, dist[2]=-3 (2 + -5), dist[3]=-2 (-3 + 1), dist[4]=0 (-2 + 2). The cycle 1->2->3->4->1 has total weight 0, so no negative cycle.
