Convert Prices Between Currencies Using Known Exchange Rate Pairs
Last updated: November 29, 2026
Quick Overview
Given known exchange rates as (from, to, rate) triples and a list of conversion queries, return the rate for each query by chaining known rates, or -1.0 if the currencies are not connected.
Amazon
November 29, 20260
0
0 solved
Given known exchange rates as (from, to, rate) triples and a list of conversion queries, return the rate for each query by chaining known rates, or -1.0 if the currencies are not connected.
An Amazon technical screen graph question themed on displaying marketplace prices in the shopper's local currency. The interviewer checks that you model rates as a weighted directed graph with reciprocal edges and multiply along a BFS or DFS path.
What the Interviewer Expects
- Model currencies as nodes with weighted edges
- Add the reciprocal edge 1/rate for each pair
- Multiply weights along a traversal path
- Return -1.0 for unknown currencies or disconnected pairs
- Discuss precomputing with weighted union-find for many queries
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if the given rates are inconsistent (an arbitrage cycle)?
- How would you answer millions of queries quickly?
- How should floating point error be handled for long chains?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: rates list of [a, b, r] meaning 1 unit of a equals r units of b; queries list of [x, y]. Output: list of floats, the rate from x to y or -1.0. A currency queried against itself returns ...
High-Level Design
Build an adjacency map: for each rate add a -> b with weight r and b -> a with weight 1/r. For each query, BFS from x carrying the product of weights so far; the first time y is reached, that prod...