KatInterview

Best Conversion Product Over Simple Currency Paths

Trees & GraphsAuto-graded · 17 test cases
MediumAuto-gradedTrees & Graphsgraphdfsbacktracking

Last updated

Find the largest product of edge weights over simple directed paths between two nodes, using backtracking DFS, with a special case for identical endpoints and an unreachable case.

Best Conversion Product Over Simple Currency Paths

A currency market has num_currencies currencies, labeled 0 to num_currencies - 1. Some ordered pairs have a direct conversion offer, given as a triple [u, v, r]. Converting 1 unit of currency u yields exactly r units of currency v. Offers work in one direction only. An offer from u to v does not give you an offer from v to u.

You can chain offers. If you convert u -> v at rate r1 and then v -> w at rate r2, the combined rate for that route is r1 * r2.

A route must be simple: it may not visit the same currency twice. This keeps the best result finite even when the offers contain profitable cycles.

You start with 1 unit of currency start and want as much of currency target as possible.

Task

Return the largest combined rate over all simple routes from start to target.

  • If start == target, return 1.0. The empty route is the only simple route, and no cycle may be used.
  • If no route leads from start to target, return -1.0.
  • The same ordered pair may appear in several offers with different rates. Any of them may be used, so the best one for that step is the one that gives the best overall product.
  • A route that ends up with less than 1 unit still counts. Return the maximum product even if it is below 1.

Function

Implement max_exchange_rate(num_currencies, rates, start, target), where:

  • num_currencies is the number of currencies.
  • rates is a list of [u, v, r] offers.
  • start and target are the starting and ending currencies.

It returns a float: the best achievable amount of target, or -1.0 if unreachable.

Constraints

  • 1 <= num_currencies, small enough that exploring all simple routes is feasible (around 15 or fewer).
  • 0 <= len(rates); duplicate pairs and self-loops (u == v) may appear. Self-loops can never be part of a simple route.
  • 0 <= u, v, start, target < num_currencies.
  • Every rate r is a positive real number.
  • Results are compared as floating point numbers, so tiny rounding differences are acceptable.

Examples

Example 1

Input: max_exchange_rate(3, [[0, 1, 2], [1, 2, 3], [0, 2, 4]], 0, 2)

Output: 6

Hints

Hint 1

Because routes may not repeat a currency, the best route is the maximum over a finite set of simple paths. Think about enumerating them.

Hint 2

Run a depth-first search from the start while carrying the product so far, and track which currencies are on the current route. Un-mark a currency when you backtrack so other routes can use it.

Hint 3

Handle start == target before searching (answer 1.0), and use a sentinel such as None to tell 'no route found' apart from a legitimately small product.