Detect Currency Arbitrage After a Cycle Fee
OptiverSoftware Engineer · Technical ScreenLast updated
You are given a matrix of positive currency exchange rates, where rates[i][j] units of currency j are received for one unit of currency i. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.
Detect Currency Arbitrage After a Cycle Fee
You are given a matrix of positive currency exchange rates, where rates[i][j] units of currency j are received for one unit of currency i. Determine whether any exchange sequence of between 1 and n steps starts and ends at the same currency and multiplies the starting amount by more than 1.0001. The threshold represents a fee equal to 0.01 percent of the starting amount charged once for the completed sequence.
Function Contract
Implement has_arbitrage(rates) -> bool.
Constraints
- 1 <= number of currencies n <= 50.
- The matrix is square and every rate is a positive finite number.
- Currencies may repeat, but the candidate sequence contains at most n exchanges.
- Comparisons should tolerate only ordinary floating-point roundoff, not erase the stated 1.0001 threshold.
Examples
rates = [[1.0, 0.5], [2.0, 1.0]]
output = false
rates = [[1.0, 2.0], [0.51, 1.0]]
output = true
Hints
Hint 1
Transform products: Taking logarithms converts multiplication along an exchange sequence into addition.
Hint 2
Bound the path state: For each start currency and step count, track the best log-value that reaches every current currency.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.