← Questions

Best currency conversion rate across exchange quotes

0
UberStripeCodingLast asked Nov 2025

A coding interview question asked in Uber's technical phone screen and at Stripe, last asked Nov 2025. It tests graphs, shortest path and bellman-ford.

You run a currency desk. You are given a list of quotes (from, to, rate): one unit of from buys rate units of to. A quote works only in the direction given; the reverse is available only if it is quoted separately (usually at a slightly worse rate).

Implement:

class CurrencyExchange:
    def __init__(self, quotes: list[tuple[str, str, float]]) -> None: ...
    def best_rate(self, src: str, dst: str) -> float: ...

best_rate returns the largest amount of dst you can get for one unit of src, converting through any number of intermediate currencies. Return -1 if dst cannot be reached or either currency is unknown, and 1 when src == dst.

Assumptions:

  • All rates are positive. There can be several quotes and several routes between the same two currencies.
  • The market has no arbitrage: no cycle of conversions turns one unit of a currency into more than one unit of itself. (State what your code does if this is violated.)
  • Up to a few hundred currencies and a few thousand quotes.

Example 1

quotes = [("CAD","USD",0.73), ("USD","CHF",0.88), ("CAD","CHF",0.62)]
best_rate("CAD", "CHF") -> 0.6424   # CAD -> USD -> CHF beats the direct 0.62
best_rate("CHF", "CAD") -> -1       # no quote leaves CHF

Example 2

quotes = [("SGD","INR",61.0), ("INR","KRW",16.2), ("SGD","KRW",990.0)]
best_rate("SGD", "KRW") -> 990.0    # the direct quote beats 61 * 16.2 = 988.2
best_rate("SGD", "SGD") -> 1.0

Follow-ups may ask for the conversion path, many queries against one rate table, a limit on the number of hops, or detecting arbitrage.

Clarifying questions

  • If two quotes exist for the same pair, should I keep only the better rate, or can both matter?
  • Is a quote's reverse ever implied, or strictly only when quoted separately?
  • Are queries many against a fixed table, or does the table change between queries?
  • How should floating-point results be compared: is a small tolerance acceptable?
  • If an arbitrage cycle does exist, should I raise, return a sentinel, or cap the number of hops?

What a strong answer covers

  • Models currencies as nodes and quotes as directed weighted edges, keeping the best rate per pair.
  • Finds the maximum product of rates as a shortest path on weights -log(rate): Bellman-Ford (or SPFA) in O(V * E), or a DFS/BFS over simple paths only when the graph is tiny.
  • Explains why plain Dijkstra is wrong here: rates above 1 give negative weights (Example 2's 61.0), while no arbitrage means no negative cycle, so Bellman-Ford is correct and its extra pass detects arbitrage if the assumption breaks.
  • Handles src == dst (1), unknown currencies and unreachable targets (-1), and the case where a multi-hop route beats the direct quote (Example 1) or loses to it (Example 2).
  • Tests a direction-only quote, parallel quotes, a disconnected currency and an injected arbitrage cycle.

Follow-ups

  • Return the conversion path, not just the rate.
  • Answer many queries fast: precompute all pairs (Floyd-Warshall on logs) or cache per source.
  • Limit the route to at most k hops.
  • Detect and report an arbitrage cycle in the quotes.

Where it's asked

  • Uber · Technical phone screen · Last asked Nov 202560 min · One medium problem with a follow-up, or two short ones; sometimes object-oriented design
  • Stripe · Onsite coding: debug, integrate, build · Last asked Sep 202560 min · Project editor with starter code and tests: fix bugs in an unfamiliar codebase, wire an external API spec into a service, or complete a skeleton in parts · Varies by session: some onsites have one AI-assisted session and an integration session without AI

What it tests

graphs · shortest path · bellman-ford

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding