Shortest Path in Weighted Graph Using Dijkstra’s Algorithm

Asked atMcKinsey & Company
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Build an adjacency list and set every distance to infinity except the source.
  2. 02Push source into a min-heap keyed by tentative distance.
  3. 03Pop the least distance, skip stale entries, and relax outgoing edges.
  4. 04Optionally retain predecessor nodes and translate unreachable infinity values.

Reference answer

Then expect these follow-ups

  • How would you return the path to a target?

    Tests: predecessors

  • What changes for negative edges?

    Tests: Bellman-Ford

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question