Not sure what to study next?

Get a personalised quant interview roadmap built around your upcoming interviews, strengths and weaknesses.

Takes a few minutes. Start with your target role and firms.

← Programming questions
Hard / PYTHON

Dijkstra: Shortest Path

graphs · heaps · shortest_path 35 minute study target

Not completed

Challenge

Implement dijkstra(graph, start, end). The directed graph is a dictionary mapping string node labels to dictionaries of neighbour labels and non-negative numeric edge weights. Every referenced neighbour is also a key in graph. Return the minimum sum of edge weights from start to end, or the exact string "invalid" if either node is absent or the destination is unreachable. A node has distance 0 from itself. Zero-weight edges and cycles are allowed; negative weights are outside the input contract.

For {"A":{"B":0.2},"B":{"C":0.3},"C":{}}, the distance from A to C is 0.5. Do not round intermediate distances.

Target complexity: O((V + E) log V)

Return values are checked by type. Use the exact function name. Print output is not your answer. Tests have time and memory limits.

Public examples

Example 1

Arguments

[{'A': {'B': 0.2}, 'B': {'A': 0.2, 'C': 0.3}, 'C': {'A': 0.5, 'B': 0.3}}, 'A', 'C']

Expected return

0.5
Example 2

Arguments

[{'A': {'B': 1}, 'B': {'A': 1}}, 'A', 'B']

Expected return

1
Hint

Explore the smallest tentative distance first.

The worked solution unlocks after completion, or with Premium access.

dijkstra.pyPython 3.12

Drafts are saved to your account, not to shared browser storage.

Sign in to submit code and save progress.

Results

Your results will appear here.

Hidden tests reveal only their pass/fail status, never their inputs or expected outputs. Runtime is observed wall time—not an inferred complexity class.

Join our Newsletter and Receive the Latest Updates!

Main Links

Trade Courses Assessments Interview Prep Contact Us

Social Media

Facebook Instagram YouTube

Legal

Terms and Conditions Privacy Policy Privacy settings

Talent Acquisition

Looking to hire exceptional quant talent?
See how we can help.

© 2026 EverythingQuant. All Rights Reserved.