Compute maximum currency exchange rate
Two SigmaSoftware Engineer · Technical ScreenLast updated
Compute maximum currency exchange rate evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Compute maximum currency exchange rate
You are given a set of currencies and the direct exchange rates between some pairs of them. Each direct exchange rate tells you how many units of one currency you can get for 1 unit of another currency.
Formally:
- There are
ncurrencies labeled from0ton - 1. - You are given
mdirected exchange rates. Each rate is a triple(u, v, r)meaning: - You can directly exchange from currency
uto currencyv. - If you start with
1unit of currencyu, you can obtainrunits of currencyvin a single exchange. - You may perform a sequence of exchanges, chaining them together. If you go from
u -> vwith rater1and thenv -> wwith rater2, the resulting effective rate fromutowalong that path isr1 * r2. - You should assume that you do not revisit the same currency in a single exchange path (i.e., paths are simple, without repeated nodes), so the maximum rate is always finite.
You are also given two currency labels A (start) and B (target). You start with 1 unit of currency A.
Task
Compute the maximum possible amount of currency B you can obtain from 1 unit of currency A by performing zero or more exchanges.
- If
A == B, you may choose to perform zero exchanges, in which case you can always end with at least1unit ofB, so the minimum answer is1.0. - If it is impossible to reach currency
Bstarting fromAvia any sequence of exchanges, return-1.0.
Input format (one possible specification)
- Integer
n— number of currencies. - Integer
m— number of direct exchange rates. - Next
mlines: each containsu v r: uandvare integers in[0, n-1].ris a real number> 0representing the direct exchange rate fromutov.- Last line: two integers
A B— the start and target currencies.
Output format
- Output a single real number: the maximum amount of currency
Bobtainable from1unit of currencyAusing any sequence of allowed exchanges. - If
Bis unreachable fromA, output-1.0.
Example
Suppose:
n = 3(currencies0,1,2)m = 3- Exchange rates:
0 1 2.0(1 unit of0→ 2 units of1)1 2 3.0(1 unit of1→ 3 units of2)0 2 4.0(1 unit of0→ 4 units of2)A = 0,B = 2.
Possible paths from 0 to 2:
- Direct:
0 -> 2with rate4.0→ get4.0units. - Via
1:0 -> 1 -> 2with rate2.0 * 3.0 = 6.0→ get6.0units.
The answer is 6.0, the maximum achievable amount of currency 2.
Clarifying Questions to Ask
- Clarify input sizes, value ranges, mutability, return format, and tie-breaking.
- State the target time and space complexity before coding.
- Call out edge cases such as empty inputs, duplicates, invalid values, overflow, and boundary sizes.
Follow-up questions
- How would the approach change if the input were streaming or too large for memory?
- What invariants would you assert in production code?
- Which tests would catch off-by-one, duplicate, or tie-breaking bugs?
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.