KatInterview

Multi-Hop Currency Conversion Over a Rate Table

Trees & GraphsAuto-graded · 25 test cases
MediumAuto-gradedTrees & Graphsgraphbfshash-map

Last updated

Compute a conversion factor between two currencies by chaining and inverting listed rates, returning -1.0 when no chain exists; tests graph traversal with multiplicative weights.

Multi-Hop Currency Conversion

A exchange desk keeps a list of quotes. Each quote is a triple (base, quote, rate) stating that one unit of base can be exchanged for rate units of quote. For instance, ("USD", "GBP", 0.77) says 1 USD is worth 0.77 GBP.

You are given the list of quotes, a source currency and a target currency. Return the number of units of target that one unit of source is worth. The pair you need may not be quoted directly, so the conversion may have to pass through one or more intermediate currencies.

Rules

  • Every quote works in both directions. If (A, B, r) is listed, then 1 B is also worth 1 / r units of A.
  • Rates multiply along a chain. If 1 A is worth x units of B and 1 B is worth y units of C, then 1 A is worth x * y units of C.
  • The quotes are mutually consistent: any two chains linking the same pair of currencies give the same rate (up to floating-point rounding). The correct answer is therefore unique.
  • If source == target, return 1.0, even when that currency appears in no quote.
  • Otherwise, if no chain of quotes links source to target (including when either one is absent from the quotes), return -1.0.
  • A positive answer is accepted if its relative error from the exact value is at most 1e-6. The value -1.0 must be returned exactly when no conversion is possible.

Input

  • rates: a list of triples (base, quote, rate) with string currency codes and a float rate. Entries may be given as lists.
  • source: the starting currency code.
  • target: the destination currency code.

Output

A float: the conversion factor from source to target, or -1.0 if unreachable.

Constraints

  • 0 <= len(rates) <= 10000
  • Every currency code (including source and target) is a string of 3 uppercase English letters.
  • In each quote base != quote, and every unordered pair of currencies appears in at most one quote.
  • 1e-3 <= rate <= 1e3 for every quote.
  • The exact rate between any two connected currencies is between 1e-9 and 1e9, so intermediate products never overflow or underflow a 64-bit float.

Examples

Example 1

Input: convert_rate([['USD', 'GBP', 0.77], ['GBP', 'EUR', 1.2]], 'USD', 'EUR')

Output: 0.9239999999999999

Example 2

Input: convert_rate([['USD', 'GBP', 0.77], ['GBP', 'EUR', 1.2]], 'EUR', 'USD')

Output: 1.0822510822510822

Example 3

Input: convert_rate([['USD', 'GBP', 0.77], ['JPY', 'KRW', 9.5]], 'USD', 'KRW')

Output: -1

Hints

Hint 1

Think of each currency as a node and each quote as a connection that can be walked in either direction.

Hint 2

Because the quotes are consistent, you do not need the best or shortest chain: any chain gives the right answer, so a simple traversal is enough.

Hint 3

While traversing, remember for every reached currency how many units of it one unit of the source is worth. Check source == target before you check whether the currencies appear in the table.