KatInterview

Best Multi-Hop Exchange Rate Between Two Currencies

Trees & GraphsAuto-graded · 15 test cases
MediumAuto-gradedTrees & Graphsgraphbellman-fordshortest-path

Last updated

Find the largest product of directed exchange rates along any path between two currencies, using Bellman-Ford-style relaxation on a graph that may contain cycles and rates above 1.

Best Multi-Hop Exchange Rate

A currency exchange publishes a list of directed conversion rates. Each entry is a triple [from, to, rate], meaning one unit of from can be converted into rate units of to. Converting along several consecutive entries multiplies their rates together.

Write a function that returns the largest total conversion factor obtainable when going from a starting currency to a destination currency, using any sequence of the given entries.

Rules

  • Entries are directed. An entry ["USD", "EUR", 0.9] does not let you convert EUR back to USD, and you must not infer a reverse rate.
  • The value of a path is the product of the rates of its entries. Return the maximum over all paths from the start currency to the destination currency.
  • If the start and destination currency are the same, return 1.0, even if that currency does not appear in any entry.
  • If the destination cannot be reached from the start (including when either currency never appears in the entries, or rates is empty), return 0.0.
  • There may be several entries between the same ordered pair of currencies. Any of them may be used, so the best one counts.
  • Rates can be smaller or larger than 1.
  • The graph may contain cycles, but no cycle has a product greater than 1. Looping back through a currency never improves the result.
  • Return a floating-point number. Tiny rounding differences are natural, so multiply the rates along the path as given.

Function

Implement best_exchange_rate(from_currency, to_currency, rates):

  • from_currency: string, the starting currency.
  • to_currency: string, the destination currency.
  • rates: list of [from, to, rate] entries, with string currencies and a positive float rate.
  • Returns: the maximum achievable product as a float, 1.0 if the two currencies are equal, or 0.0 if unreachable.

Constraints

  • Every rate is a positive number.
  • No cycle in the graph has a rate product strictly greater than 1.
  • The number of distinct currencies and entries is small enough for an O(V * E) solution.
  • Currency names are case-sensitive strings.

Examples

Example 1

Input: best_exchange_rate('USD', 'GBP', [['USD', 'EUR', 0.9], ['EUR', 'GBP', 0.8]])

Output: 0.7200000000000001

Hints

Hint 1

Model each currency as a node and each entry as a directed edge. The value of a path is the product of its edge rates, and you want the maximum product.

Hint 2

Plain Dijkstra on products only works when every rate is at most 1. Rates above 1 behave like negative weights in log space, so use a Bellman-Ford style relaxation: best[to] = max(best[to], best[from] * rate).

Hint 3

Because no cycle is profitable, an optimal path never repeats a currency, so at most V-1 rounds of relaxation are needed. Stop early when a round changes nothing, and handle the equal-currency case separately.