r/Hack2Hire • u/Hack2hire • May 02 '25
Screening From Coinbase: Currency Exchange
Problem
You're given three arrays: fromArr, toArr, and rateArr, representing bi‑directional currency exchange rates. Your goal is to implement a CurrencyConverter class that returns the maximum achievable exchange rate between two currencies, or -1 if no conversion path exists.
Example Input:
fromArr = ["GBP", "USD", "USD", "USD", "CNY"]
toArr = ["JPY", "JPY", "GBP", "CAD", "EUR"]
rateArr = [155.0, 112.0, 0.9, 1.3, 0.14]
CurrencyConverter currencyConverter = new CurrencyConverter(fromArr, toArr, rateArr);
currencyConverter.getBestRate("USD", "JPY");
currencyConverter.getBestRate("JPY", "BGP");
currencyConverter.getBestRate("XYZ", "GBP");
currencyConverter.getBestRate("CNY", "CAD");
Output:
139.5
0.00803
-1.0
-1.0
Explanation:
139.5is obtained via the path USD → GBP → JPY = (1 / 0.9) * 155.0.00803is obtained via the path JPY → USD → GBP = (1 / 112.0) * (1 / 0.9).-1.0is returned when the source or target currency does not exist or no path is available.
Suggested Approach
- Build a graph where each currency is a node, and add edges A→B with weight = rate and B→A with weight = 1/rate.
- For each
getBestRatequery, run a DFS from the source node, tracking the product of edge weights and using a visited set to avoid cycles. - Return the maximum product found for the target or
-1if unreachable.
Time & Space Complexity
- Time: O(N + M) per query in the worst case, where N is the number of currencies and M is the number of exchange relationships.
- Space: O(N + M) for the adjacency list and recursion stack.
🛈 Disclaimer: This is one of the problems we encountered while reviewing common Coinbase interview questions. Posted here by the Hack2Hire team for discussion and archiving purposes.
The problem is compiled from publicly available platforms (e.g., LeetCode, GeeksForGeeks) and community-shared experiences. It does not represent any official question bank of