FX Rates and Arbitrage
Prerequisites: Trade Lifecycle Overview
A firm that trades AAPL in New York and other listings in Frankfurt and Tokyo holds cash in dollars, euros, and yen, and its FX router converts between them. Build the router's quote graph.
- Quote: a dealer's rate for one direction. A USD → EUR rate of
0.9means 1 USD buys 0.9 EUR. - Conversion path: converting through other currencies multiplies the rates along the way. USD → EUR at
0.9followed by EUR → JPY at130turns 1 USD into 117 JPY, more than a direct USD → JPY quote of110gives.
What to implement
process_commands(currencies, commands)returns one line for everyQUERYandARBITRAGEcommand, in order.currenciesholds theCdistinct currency codes, and each command is one line.- On stdin, the first line lists the codes separated by spaces, the second line holds the number of commands, and the commands follow, one per line.
- There are two parts, and Part 2 adds behavior on top of Part 1. One solution must pass all eleven tests.
| Command | What it does |
|---|---|
RATE A B r | Set the rate from A to B (prints nothing) |
QUERY A B | Print the best rate from A to B |
ARBITRAGE | Print whether a profitable cycle exists (Part 2) |
Rates and rounding
- Every rate
ris a positive decimal with at most six digits after the point, andr ≤ 10^6. - The expected answers come from exact arithmetic: treat each rate as the exact decimal it spells, and multiply and compare products exactly.
- A printed rate is rounded to exactly six decimal places with halves rounded up, so an exact
2.5000005would print as2.500001.
Printing with a double
- A
doublecannot hold most decimals exactly. It stores the nearest binary fraction (0.9becomes 0.90000000000000002220…), and every multiplication rounds again, so a computed product can be off around its sixteenth significant digit. - The tests leave room for that error. Every printed rate is below
10^6and at least10^-7away from a halfway point between two six-decimal values (such as2.5000005, halfway between2.500000and2.500001). - So printing a
doubleproduct with six decimals prints the exact answer: usestd::fixedandstd::setprecision(6)in C++ orf"{x:.6f}"in Python.
Comparing cycle products with 1 (Part 2)
- Here a
doubleneeds a tolerance. In the tests, no cycle that visits each currency at most once has a product strictly between 1 and 1.000001, but a cycle whose exact product is 1 can still compute as 1.0000000000000002. - A
doublesolution should therefore treat a cycle as profitable only when it beats 1 by more than a small factor. The reference solution uses1 + 1e-9. - Exact rational arithmetic, such as Python's
fractions.Fraction, needs no tolerance.
Part 1: Best conversion
RATE A B rsets the rate fromAtoBtor, replacing any earlier quote in that direction. The quote fromBtoAis separate and does not change.QUERY A Basks for the best rate fromAtoB: the largest product of rates along a conversion path that starts atA, follows quotes in their stated direction, and ends atB.- A path may pass through any currency, even more than once.
- The empty path converts a currency to itself at rate 1.
Output of QUERY: one line.
UNREACHABLE rate
UNREACHABLEif no path leads fromAtoB.- Otherwise
rateis the best rate with six decimals.
Limits and guarantees
1 ≤ C ≤ 50, and there are at most2 × 10^5commands.- Across the input,
C^3 × R ≤ 2 × 10^7, whereRcounts theQUERYcommands plus eachARBITRAGEcommand that is the first since the latestRATE. - That budget allows
O(C^3)work per query. It expects a repeatedARBITRAGEwith noRATEin between to reuse the previous answer. - In this part, no cycle of quotes has a product greater than 1. Going around a cycle never raises a rate, so a best path needs at most
C - 1quotes. Part 2 drops that promise.
Tests 1–7 cover this part. Test 7 contains the cycle USD → EUR → GBP → USD with rates 0.8, 0.8, and 1.5625, whose exact product is 1.
Part 2: Detect profitable cycles
Quotes may now form a profitable cycle: a cycle of quotes whose product is greater than 1. Converting once around it ends with more of the starting currency than it began with, which is an arbitrage.
ARBITRAGEprintsYESwhen any profitable cycle exists andNOotherwise.QUERY A BprintsUNBOUNDEDwhen some profitable cycle can be reached fromAand can itself reachB. Each extra trip around that cycle multiplies the rate again, so no largest product exists.- A profitable cycle that
Acannot reach, or that cannot reachB, does not change the answer. - Compare cycle products with 1 exactly, as described under Rates and rounding.
Tests 8–11 cover this part.
USD EUR JPY 4 RATE USD EUR 0.900000 RATE EUR JPY 130.000000 RATE USD JPY 110.000000 QUERY USD JPY
117.000000
The first line lists three currencies, and the second says four commands follow. The direct USD → JPY quote is 110, but the path USD → EUR → JPY gives 0.9 × 130 = 117 JPY per USD.
The query prints the larger product with six decimals. No quote leads into USD, so a QUERY JPY USD at this point would print UNREACHABLE.