Problem · Graph

Currency Conversion Rate

Learn this problem
MediumAmazon logoAmazonPHONE SCREEN
See Amazon hiring insights

Problem statement

You are given currency conversion rates. Each rate has a source currency, a target currency, and the value of one unit of the source currency in the target currency.

Conversion Rules

Conversion can use multiple rates, and each listed rate can also be used in reverse by taking its reciprocal.

Given a query [from, to], return the conversion rate from from to to, rounded and formatted with exactly two digits after the decimal point.

Function

findConversionRate(rates: String[][], query: String[]) → String

Examples

Example 1

rates = [["USD","JPY","110"],["USD","AUD","1.45"],["JPY","GBP","0.0070"]]query = ["GBP","AUD"]return = "1.89"

Use the reverse of JPY -> GBP, then the reverse of USD -> JPY, then USD -> AUD: (1 / 0.0070) * (1 / 110) * 1.45 = 1.883..., which rounds to 1.89.

Example 2

rates = [["USD","CAD","1.30"],["CAD","EUR","0.70"]]query = ["USD","EUR"]return = "0.91"

One USD is 1.30 CAD, and one CAD is 0.70 EUR, so the rate is 1.30 * 0.70 = 0.91.

More Amazon problems

drafts saved locally
public String findConversionRate(String[][] rates, String[] query) {
  // write your code here
}
rates[["USD","JPY","110"],["USD","AUD","1.45"],["JPY","GBP","0.0070"]]
query["GBP","AUD"]
expected"1.89"
checking account