TL;DR: The Bellman Ford algorithm finds single-source shortest paths in a weighted directed graph, even when some edges have negative weights. Initialize the source distance to zero, all others to infinity, relax every edge up to V minus 1 times, then make one extra pass: any further improvement proves that a reachable negative-weight cycle exists.
Bellman Ford Algorithm: Coding Interview Guide
Learn the Bellman Ford algorithm, trace edge relaxation, detect negative cycles, explain complexity, and write interview-ready pseudocode.
Try YesToTheOffer
What is the Bellman Ford algorithm?
Bellman Ford is a dynamic-programming-style shortest-path algorithm. After the first full pass, the best known paths use at most one edge; after the second, at most two edges. A simple path contains at most V minus 1 edges, which explains both the loop count and the correctness argument. Unlike Dijkstra's algorithm, Bellman Ford does not assume nonnegative edge weights.
How do you explain edge relaxation?
For an edge from u to v with weight w, relaxation asks whether distance[u] + w is smaller than distance[v]. Only perform the addition when u is reachable. If the candidate is better, update distance[v] and set predecessor[v] to u. The predecessor array is optional for distances, but it lets you reconstruct the actual path and explain your result clearly.

How do you trace Bellman Ford in an interview?
Use a small graph and write one distance row per pass. Start with source A at 0 and every other vertex at infinity. Scan the complete edge list in a consistent order, recording each update. Stop early when a whole pass makes no changes. Be explicit that edge order can change intermediate rows but not the final correct distances when no reachable negative cycle exists.
What pseudocode should you write?
Create distance and predecessor arrays, then repeat a full edge scan V minus 1 times. Use a changed flag for early termination. Finally, scan all edges once more and report a negative cycle if a reachable distance can still improve. In production code, choose a numeric type that can hold path sums and guard the infinity sentinel before addition.
What are the time and space complexities?
The standard adjacency-edge-list implementation runs in O(VE) time because it can scan E edges on each of V minus 1 passes, plus one detection pass. It uses O(V) auxiliary space for distances and predecessors. Early stopping improves favorable inputs, but the worst-case bound remains O(VE).
| Interview decision | Use Bellman Ford when | Prefer another approach when |
|---|---|---|
| Edge weights | Negative edges may occur | All edges are nonnegative and speed matters |
| Cycle requirement | Detect a reachable negative cycle | Cycle detection is not required |
| Complexity | O(VE) is acceptable | The graph is too large or dense for repeated scans |
When should you choose Bellman Ford instead of Dijkstra?
Choose Bellman Ford when negative edge weights are permitted or when the interviewer explicitly asks for reachable negative-cycle detection. Choose Dijkstra with a priority queue for graphs whose edge weights are all nonnegative because it is normally faster. For all-pairs shortest paths, first clarify graph density, negative weights, and whether paths or only distances are required.
Which mistakes should you avoid?
Do not relax from an unreachable vertex, confuse a negative edge with a negative cycle, or claim that every negative cycle invalidates the result: only a cycle reachable from the source affects its shortest paths. Also avoid running only V minus 2 passes, skipping the final detection pass, or reconstructing a path without maintaining predecessors.
How can you practice the explanation?
Practice three versions: a 30-second definition, a two-minute correctness explanation, and a complete implementation. Test an unreachable vertex, one negative edge without a cycle, a reachable negative cycle, and a graph that stabilizes early. This separates memorized code from genuine understanding.
Use AI to organize material you already understand, rehearse explanations, and review your performance. Follow the employer's and assessment provider's rules, and never invent experience, numbers, or results.
Continue with the AI interview copilot guide, resume-grounded preparation, and post-interview review workflow.
Frequently asked questions
FAQ
Can Bellman Ford handle negative weights?
Yes. It can handle negative edge weights. It cannot produce finite shortest paths for vertices affected by a reachable negative-weight cycle, which is why the extra relaxation pass is essential.
Why does Bellman Ford run V minus 1 times?
Any simple path has at most V minus 1 edges. After pass k, the algorithm has considered shortest paths using at most k edges, so V minus 1 passes cover every simple shortest path.
How does Bellman Ford detect a negative cycle?
After the normal passes, scan every edge once more. If a reachable distance can still decrease, a reachable negative-weight cycle exists because a simple path should already be finalized.
What is Bellman Ford's complexity?
The standard algorithm uses O(VE) time and O(V) auxiliary space. An early-exit flag can stop the loop when a full pass makes no updates, but it does not change the worst-case bound.
Is Bellman Ford better than Dijkstra?
Neither is universally better. Bellman Ford supports negative weights and cycle detection; Dijkstra is generally faster when all edge weights are nonnegative.
Practice with evidence, not a script
YesToTheOffer can ground preparation and real-time answer structure in your resume, role description, and private notes, then preserve a transcript for post-interview review.
Practice with evidence, not a script
YesToTheOffer can ground preparation and real-time answer structure in your resume, role description, and private notes, then preserve a transcript for post-interview review.
Try YesToTheOffer