← 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
Hiring now
Get the McKinsey & Company interview, not just the prep
landed applies to roles like these for you, on each company’s own site, with answers written for the role.
Get 5x interviews10 free auto-applies. No card.
Next question