Currency Conversion Rate
Updated
Problem statement
You are given currency conversion rates. Each row [from, to, rate] says that one unit of from is worth rate units of to. A listed rate may also be used in reverse by taking its reciprocal, and a conversion may use several rates.
Answer one query. A two-field query [from, to] converts one unit, preserving the unit-rate interface. A three-field query [from, to, amount] converts the given amount. Rate and amount fields are decimal strings.
Return the converted amount, rounded and formatted with exactly two digits after the decimal point. Rates are consistent: every path between the same currencies gives the same exact conversion factor. Cycles may exist. Both query currencies are known and connected; converting a currency to itself keeps the amount unchanged.
Practice follow-up: repeated queries
Extension: discuss how preprocessing can be reused to answer many queries against the same rate table. The editorial includes weighted union-find for this extension. Each judged function call still answers one query and does not retain state between calls.
Function
findConversionRate(rates: String[][], query: String[]) → StringExamples
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.
Example 3
rates = [["USD","EUR","0.9"],["EUR","JPY","100"],["USD","JPY","90"]]query = ["JPY","USD","450"]return = "5.00"Use reverse edges from JPY to EUR, then to USD: 450 / 100 / 0.9 = 5. The direct reverse edge gives 450 / 90 = 5 as well. Return "5.00".
Constraints
1 <= rates.length <= 2000.- Every rate row has exactly
3fields;2 <= query.length <= 3. - Currency names contain
1to10uppercase ASCII letters or digits. - Rate and amount fields are nonnegative decimal text with at most
6digits after the decimal point; every rate is positive. - Each supplied rate and every exact conversion factor between connected currencies is between
0.000001and1000000. - The query amount is between
0and1000000; a two-field query uses amount1. - Rates are consistent, and both query currencies occur in the rate table and are connected.
- The exact converted amount is at most
1000000and is at least0.0001away from every midpoint between consecutive cents.
More Amazon Phone Screen questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Shortest Pin Path Through TopicsPinterest · PHONE SCREENPinterest interview questions
- Word Chain Jumps in a GridGoogle · PHONE SCREENGoogle interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Hierarchical Permission CheckeBay · ONSITE INTERVIEWeBay interview questions
- Route Graph Reachability with a Broken LinkFigma · PHONE SCREENFigma interview questions
- Minimum Currency ConversionsEPAM · PHONE SCREENEPAM interview questions
- Finite Web CrawlerMongoDB · PHONE SCREEN · ONSITE INTERVIEWMongoDB interview questions
- Currency Conversion RateBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Currency Conversion RateMercor · ONSITE INTERVIEWMercor interview questions
- Clone a Connected GraphAbridge · PHONE SCREENAbridge interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)