← All questions
Coding
Shortest Path in Weighted Graph Using Dijkstra’s Algorithm
Asked at
McKinsey & 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+
- 01Build an adjacency list and set every distance to infinity except the source.
- 02Push source into a min-heap keyed by tentative distance.
- 03Pop the least distance, skip stale entries, and relax outgoing edges.
- 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