Best Currency Round Trip Over Two Days
Updated
Problem statement
You hold 1 unit of the currency startCurrency. You may trade currencies on two days, first on day 1 and then on day 2.
On day 1, dayOnePairs[i] = [from, to] with rate dayOneRates[i] means one unit of from buys dayOneRates[i] units of to. The trade also works backward: one unit of to buys 1 / dayOneRates[i] units of from. Day 2 works the same way with dayTwoPairs and dayTwoRates.
Each day you may make any number of trades (including none) using only that day's pairs. You always hold your whole amount in one currency. Return the largest amount of startCurrency you can hold at the end of day 2.
Your answer is accepted if it differs from the exact value v by at most 10^-5 * max(1, |v|).
Function
bestTwoDayAmount(startCurrency: String, dayOnePairs: String[][], dayOneRates: double[], dayTwoPairs: String[][], dayTwoRates: double[]) → doubleExamples
Example 1
startCurrency = "USD"dayOnePairs = [["USD","CAD"],["CAD","MXN"]]dayOneRates = [1.5,9.6]dayTwoPairs = [["MXN","USD"],["USD","CAD"]]dayTwoRates = [0.1,1.2]return = 1.44On day 1: 1 USD becomes 1.5 CAD, then 1.5 × 9.6 = 14.4 MXN. On day 2: 14.4 MXN becomes 14.4 × 0.1 = 1.44 USD. Going through CAD instead gives only 1.5 / 1.2 = 1.25 USD on day 2. The best final amount is 1.44.
Example 2
startCurrency = "EUR"dayOnePairs = [["GBP","EUR"]]dayOneRates = [1.25]dayTwoPairs = [["EUR","GBP"]]dayTwoRates = [0.8]return = 1.0On day 1 the pair says 1 GBP buys 1.25 EUR, so 1 EUR buys 1 / 1.25 = 0.8 GBP. On day 2, 0.8 GBP buys back 0.8 / 0.8 = 1 EUR. No trade gains anything, so the answer is 1.0 (you may also make no conversions at all).
Example 3
startCurrency = "JPY"dayOnePairs = [["JPY","KRW"]]dayOneRates = [9.0]dayTwoPairs = [["CHF","SEK"]]dayTwoRates = [6.5]return = 1.0On day 1 you can turn JPY into KRW, but day 2 has no pair that leads back to JPY from KRW. Keep the 1 JPY: the answer is 1.0.
Constraints
1 <= dayOnePairs.length, dayTwoPairs.length <= 10dayOneRates.length == dayOnePairs.lengthanddayTwoRates.length == dayTwoPairs.lengthstartCurrencyand every currency in a pair is exactly3uppercase English letters, and the two currencies in a pair are different.0.1 <= rate <= 10, with at most 2 digits after the decimal point- Within one day, the pairs never form a loop of currencies, so no two pairs join the same two currencies and the rates never disagree.
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Route Graph Reachability with a Broken LinkFigma · PHONE SCREENFigma interview questions
- Page Referrer ReachabilityAmazon · ONSITE INTERVIEW
- Reachable Nodes in a Directed GraphSoFi · ONSITE INTERVIEWSoFi interview questions
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Word Chain Jumps in a GridGoogle · PHONE SCREENGoogle interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Evaluate DivisionApple · PHONE SCREENApple interview questions
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
- Validate and Resolve Evaluation ChainsPlaid · PHONE SCREEN · ONSITE INTERVIEWPlaid interview questions