FastPrepCurrency Conversion Rate

Currency Conversion Rate

Bloomberg LP logoBloomberg LP● MediumNEW GRADFULLTIMEPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

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

A conversion may use multiple rates. A listed rate may 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. Every judged query is connected by the supplied rates.

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.88"

Use the reverse of JPY -> GBP, then the reverse of USD -> JPY, then USD -> AUD: (1 / 0.0070) * (1 / 110) * 1.45 = 1.883116.... Rounded to two decimal places, the result is 1.88.

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 Bloomberg LP problems

See Bloomberg LP hiring insights
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.88"
Checking account…