Compute maximum currency exchange rate
Compute Maximum Currency Exchange Rate
You are given a set of n currencies, labeled from 0 to n - 1, and m direct exchange rates between some pairs of them. Each direct exchange rate specifies how many units of one currency you can obtain for 1 unit of another.
Formally:
- An exchange rate is represented as a triple
(u, v, r), meaning that if you start with1unit of currencyu, you can directly obtainrunits of currencyv. - You can perform a sequence of exchanges, chaining them together. If you exchange from
utovwith rater1and then fromvtowwith rater2, the effective rate fromutowalong that path isr1 * r2. - You must assume that you do not revisit the same currency within a single exchange path (i.e., paths are simple, without repeated nodes). This ensures the maximum rate is always finite.
You are also given a starting currency A and a target currency B. Your goal is to determine the maximum possible amount of currency B you can obtain, starting with 1 unit of currency A, by performing zero or more allowed exchanges.
Constraints and Edge Cases
- If
Ais the same asB(A == B), you may choose to perform zero exchanges. In this scenario, 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, you should return-1.0.
Input Format (Example Specification)
- An integer
nrepresenting the total number of currencies. - An integer
mrepresenting the number of direct exchange rates. - The next
mlines each contain three values:u v r.uandvare integers in the range[0, n-1].ris a real number greater than0, representing the direct exchange rate fromutov.
- The last line contains two integers
A B, representing the start and target currencies.
Output Format
- Output a single real number: the maximum amount of currency
Bobtainable from1unit of currencyA. - 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 maximum achievable amount of currency 2 is 6.0.
AI interview prep
Core dilemma
Finding the maximum product path in a directed graph while adhering to simple path constraints and managing floating-point precision issues.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Compute maximum currency exchange rate
Compute Maximum Currency Exchange Rate
You are given a set of n currencies, labeled from 0 to n - 1, and m direct exchange rates between some pairs of them. Each direct exchange rate specifies how many units of one currency you can obtain for 1 unit of another.
Formally:
- An exchange rate is represented as a triple
(u, v, r), meaning that if you start with1unit of currencyu, you can directly obtainrunits of currencyv. - You can perform a sequence of exchanges, chaining them together. If you exchange from
utovwith rater1and then fromvtowwith rater2, the effective rate fromutowalong that path isr1 * r2. - You must assume that you do not revisit the same currency within a single exchange path (i.e., paths are simple, without repeated nodes). This ensures the maximum rate is always finite.
You are also given a starting currency A and a target currency B. Your goal is to determine the maximum possible amount of currency B you can obtain, starting with 1 unit of currency A, by performing zero or more allowed exchanges.
Constraints and Edge Cases
- If
Ais the same asB(A == B), you may choose to perform zero exchanges. In this scenario, 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, you should return-1.0.
Input Format (Example Specification)
- An integer
nrepresenting the total number of currencies. - An integer
mrepresenting the number of direct exchange rates. - The next
mlines each contain three values:u v r.uandvare integers in the range[0, n-1].ris a real number greater than0, representing the direct exchange rate fromutov.
- The last line contains two integers
A B, representing the start and target currencies.
Output Format
- Output a single real number: the maximum amount of currency
Bobtainable from1unit of currencyA. - 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 maximum achievable amount of currency 2 is 6.0.