Convert Through Cross Rates

Hard ⏱ 18 min 26% acceptance ★★★★☆ 4.4
Rates come as a dict of direct pairs like {('USD', 'INR'): 83.0}. Write convert(rates, amount, src, dst) that uses the direct rate, the inverse of the reverse pair, or a single-hop bridge through USD when neither direct form exists — returning None if even that fails. Round to 2 decimals.

Examples

Example 1
Input
convert({('USD', 'INR'): 83.0, ('USD', 'EUR'): 0.9}, 100, 'EUR', 'INR')
Output
9222.22
Explanation

EUR→USD via inverse (÷0.9), then USD→INR (×83).

Constraints

  • Try direct, inverse, then USD bridge.
  • src == dst returns amount unchanged.

Topics

Business Logicgraph-lite

Companies

WiseRevolutWestern Union

Hints

Hint 1

Inverse: 1 / rates[(dst, src)].

Hint 2

Bridge: convert to USD, then USD to dst, reusing your own logic.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.