Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Shortest Path in Currency Graph

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Shortest Path in Currency Graph. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Wise. Energy models supported currency conversions as a directed graph. Each currency is a node, and an edge from currency A to currency B means a direct conversion is available. Given the graph, a source currency, and a target currency, return a route with the fewest conversion steps.

Implement find_shortest_conversion_path(graph, source, target) using Breadth-First Search. The returned route must include both endpoints. If the currencies are identical, return a one-element route. If no route exists, return an empty list. Treat edges as directed, and do not mutate the input graph.

If multiple shortest routes exist, return the one discovered first by BFS, processing each adjacency list in its given order.

Formal Specification

  • graph is a dictionary mapping currency codes to lists of directly reachable currency codes.
  • source and target are strings.
  • Return a list of currency-code strings representing the shortest route, or [] when no route exists.
  • A currency may appear as a destination without having its own key in graph.

Constraints

  • 1 <= number of listed currencies <= 10^5
  • 0 <= number of conversion edges <= 2 * 10^5
  • Currency codes are non-empty strings
  • The graph may contain cycles and disconnected components
  • Edges are directed and adjacency-list order determines tie-breaking

Function Signature

def find_shortest_conversion_path(graph, source, target):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output