Detect a Profitable FX Conversion Cycle Across Currency Quotes

Last updated: November 29, 2026

Quick Overview

Given a list of directed FX quotes (from currency, to currency, rate) and an optional per-conversion fee in basis points, return a cycle of currencies whose product of rates after fees exceeds 1, or None if no such cycle exists.

UBS
Coding & Algorithms
Software Engineer
UBS
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a list of directed FX quotes (from currency, to currency, rate) and an optional per-conversion fee in basis points, return a cycle of currencies whose product of rates after fees exceeds 1, or None if no such cycle exists.

UBS runs a large FX business where pricing engines aggregate quotes from many liquidity sources, and stale or mis-keyed quotes can create apparent arbitrage that must be caught before it is shown to clients. The interviewer checks that you turn a multiplicative problem into a shortest path problem and can actually recover the cycle, not just detect it.

What the Interviewer Expects
  • Transform rates with negative logarithms so a profitable cycle becomes a negative cycle
  • Use Bellman-Ford with a virtual source to cover disconnected currencies
  • Recover the cycle by walking parent pointers n steps first
  • Fold fees into edge weights
  • Use a tolerance to avoid flagging floating point noise
Key Topics to Cover
Graphs
Bellman-Ford
Negative Cycle Detection
Logarithms
FX
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you find the most profitable cycle rather than any cycle, and why is that hard?
  • Quotes update thousands of times a second: how would you avoid rerunning the full algorithm?
  • How should bid and ask quotes be represented as edges?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Each quote (A, B, r) means 1 unit of A buys r units of B. A cycle A to B to C to A is profitable when rABrBCrCA>1r_{AB} r_{BC} r_{CA} > 1. Each conversion pays a fee, so the effective rate is r(1−f)r(1 - f). Ret...

High-Level Design

Taking w=−ln⁡(r(1−f))w = -\ln(r(1-f)) turns a product greater than 1 into a sum less than 0, so a profitable cycle is exactly a negative weight cycle. Bellman-Ford finds one: initialize every distance to 0 (equiv...


Submit Your Answer
Markdown supported

Related Questions