In this problem, you start with a given amount of an initial currency. You are provided with exchange rates for pairs of currencies on Day 1, and a different set of exchange rates for pairs of currencies on Day 2. You can perform any number of currency conversions on Day 1, hold the resulting currency overnight, and then perform any number of conversions on Day 2. Your goal is to maximize the final amount of your initial currency at the end of Day 2.
This is a fantastic Graph Traversal / Shortest Path problem disguised as financial arbitrage. Interviewers ask it to test your ability to model real-world networks using directed graphs with weighted edges (exchange rates). It evaluates your understanding of Breadth-First Search (BFS) or Bellman-Ford algorithms to find the most profitable path through a network of multipliers.
This problem leverages a Two-Phase Graph Traversal (BFS or DFS) pattern.
1.0, run a BFS/DFS to find the maximum possible amount you can obtain for every other currency. Store these maximums in a Hash Map.Initial: 100 USD.
Day 1 Rates: USD -> EUR (0.9), EUR -> GBP (0.8).
Day 2 Rates: GBP -> JPY (1.2), JPY -> USD (1.5), EUR -> USD (1.1).
A common error is assuming that the best Day 1 currency is simply the one with the highest absolute numerical value. You must evaluate the entire path back on Day 2. Another mistake is ignoring cycles. While typical arbitrage problems involve negative-weight cycles (infinite money loops), these problems usually constrain the graph to be a tree or a DAG per day, meaning standard BFS/DFS with a visited set is sufficient and prevents infinite loops.
When tackling currency conversion interview problems, always represent the data as a Hash Map of Hash Maps: Map<String, Map<String, Double>> graph. This allows for ultra-fast neighbor lookups. If the problem guarantees no infinite arbitrage loops, a standard BFS keeping track of the current_amount * rate is perfectly safe and highly performant.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Alien Dictionary | Hard | Solve | |
| Apply Substitutions | Medium | Solve | |
| Evaluate Division | Medium | Solve | |
| Most Profitable Path in a Tree | Medium | Solve | |
| Shortest Distance After Road Addition Queries I | Medium | Solve |