EvalFrame Technical Mock Interviews & Coding Practice

Currency Conversion

Medium · Graph / BFS · Algorithm problem · all problems

Given a list of currency exchange rates, find the conversion rate from a starting currency (`from`) to a target currency (`to`). **Rules:** 1. Each rate represents: 1 unit of `from` = `rate` units of `to`. 2. Every rate is bidirectional: if A -> B is r, then B -> A is 1 / r. 3. Conversions can be chained: A -> B -> C = rate(A -> B) * rate(B -> C). 4. Converting a known currency to itself returns 1.0. 5. If no path exists between `from` and `to`, or if either currency was never seen in the rates list, return `null`. **Examples:** ```typescript const rates = [ { from: "USD", to: "EUR", rate: 0.9 }, { from: "EUR", to: "GBP", rate: 0.8 } ]; convertCurrency(rates, "USD", "GBP") // 0.72 convertCurrency(rates, "USD", "JPY") // null ```

Solve It in the BrowserPractice Explaining It Out Loud

Optimal Approach

  • Time complexity: O(V + E)
  • Space complexity: O(V + E)

Building the adjacency list graph visits each of the E exchange rates. In the worst case, Breadth-First Search (BFS) traverses all vertices (V currencies) and edges (E exchange connections). Overall time complexity is O(V + E).

Follow-Up Questions Interviewers Ask About Currency Conversion

  • Why is BFS generally preferred over DFS for finding conversion rates?
    BFS explores nodes level-by-level, ensuring we find the path with the fewest currency conversion hops (minimizing round-off error and friction).
  • What if exchange rates can lead to arbitrage (making free money by cycling through conversions)?
    This is the classic Negative Cycle detection problem. By taking the negative logarithm of rates (-log(rate)), currency multiplication turns into addition, allowing you to use the Bellman-Ford algorithm to detect arbitrage opportunities.
  • How would you scale this system if there are millions of historical exchange queries per second?
    Precompute all-pairs shortest paths using Floyd-Warshall or memoize calculated cross-rates, and use a caching layer (e.g. Redis) keyed by pair and timestamp.

Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.