Best FX Conversion Rate Between Currencies Within K Hops
Last updated: November 29, 2026
Quick Overview
Given directed conversion corridors `(from, to, rate)` where each rate already includes fees, find the best achievable rate from a source currency to a target currency using at most `k` conversions, along with the currency path.
Stripe
November 29, 20260
0
0 solved
Given directed conversion corridors `(from, to, rate)` where each rate already includes fees, find the best achievable rate from a source currency to a target currency using at most `k` conversions, along with the currency path.
Stripe converts between many presentment and settlement currencies, and routing through an intermediate currency is sometimes cheaper than a direct corridor, but each hop adds operational risk, so the number of hops is capped. The interviewer checks that you turn a max-product problem into a bounded Bellman-Ford, keep the hop limit exact and use exact arithmetic.
What the Interviewer Expects
- Recognize max product of rates as a shortest-path problem (equivalently, minimize the sum of negative logs)
- Use exactly `k` relaxation rounds, each reading from the previous round's values
- Track the path so the routing can be audited
- Use `Decimal` to avoid float noise when comparing near-equal routes
- Explain why Dijkstra does not directly apply when some rates exceed 1
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
- How would you detect an arbitrage cycle in the corridor graph?
- What if each corridor also has a maximum volume per day?
- How would you precompute best routes for every currency pair?
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: list of (from, to, rate_string), src, dst and k >= 1. Output: (Decimal rate, [src, ..., dst]) or None if dst is unreachable within k hops. Converting a currency to itself costs ...
High-Level Design
Keep best[c] = (rate, path), the best product reachable in at most h hops. Each round copies best into nxt and relaxes every corridor using best (the previous round), so a single round never...