Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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.9 means 1 USD buys 0.9 EUR.
  • Conversion path: converting through other currencies multiplies the rates along the way. USD → EUR at 0.9 followed by EUR → JPY at 130 turns 1 USD into 117 JPY, more than a direct USD → JPY quote of 110 gives.

What to implement

  • process_commands(currencies, commands) returns one line for every QUERY and ARBITRAGE command, in order.
  • currencies holds the C distinct 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.
CommandWhat it does
RATE A B rSet the rate from A to B (prints nothing)
QUERY A BPrint the best rate from A to B
ARBITRAGEPrint whether a profitable cycle exists (Part 2)

Rates and rounding

  • Every rate r is a positive decimal with at most six digits after the point, and r ≤ 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.5000005 would print as 2.500001.

Printing with a double

  • A double cannot hold most decimals exactly. It stores the nearest binary fraction (0.9 becomes 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^6 and at least 10^-7 away from a halfway point between two six-decimal values (such as 2.5000005, halfway between 2.500000 and 2.500001).
  • So printing a double product with six decimals prints the exact answer: use std::fixed and std::setprecision(6) in C++ or f"{x:.6f}" in Python.

Comparing cycle products with 1 (Part 2)

  • Here a double needs 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 double solution should therefore treat a cycle as profitable only when it beats 1 by more than a small factor. The reference solution uses 1 + 1e-9.
  • Exact rational arithmetic, such as Python's fractions.Fraction, needs no tolerance.

Part 1: Best conversion

  1. RATE A B r sets the rate from A to B to r, replacing any earlier quote in that direction. The quote from B to A is separate and does not change.
  2. QUERY A B asks for the best rate from A to B: the largest product of rates along a conversion path that starts at A, follows quotes in their stated direction, and ends at B.
  3. A path may pass through any currency, even more than once.
  4. The empty path converts a currency to itself at rate 1.

Output of QUERY: one line.

UNREACHABLE
rate
  • UNREACHABLE if no path leads from A to B.
  • Otherwise rate is the best rate with six decimals.

Limits and guarantees

  • 1 ≤ C ≤ 50, and there are at most 2 × 10^5 commands.
  • Across the input, C^3 × R ≤ 2 × 10^7, where R counts the QUERY commands plus each ARBITRAGE command that is the first since the latest RATE.
  • That budget allows O(C^3) work per query. It expects a repeated ARBITRAGE with no RATE in 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 - 1 quotes. 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.

  1. ARBITRAGE prints YES when any profitable cycle exists and NO otherwise.
  2. QUERY A B prints UNBOUNDED when some profitable cycle can be reached from A and can itself reach B. Each extra trip around that cycle multiplies the rate again, so no largest product exists.
  3. A profitable cycle that A cannot reach, or that cannot reach B, does not change the answer.
  4. Compare cycle products with 1 exactly, as described under Rates and rounding.

Tests 8–11 cover this part.

Example
Input
USD EUR JPY
4
RATE USD EUR 0.900000
RATE EUR JPY 130.000000
RATE USD JPY 110.000000
QUERY USD JPY
Output
117.000000
Explanation

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.