Look Up the FX Rate in Effect at a Charge Timestamp
Last updated: November 29, 2026
Quick Overview
Build a store of currency conversion rates where each rate for a currency pair has an effective timestamp. Support adding rates in any order and querying the rate that was in effect for a pair at a given time.
Stripe
November 29, 20260
0
0 solved
Build a store of currency conversion rates where each rate for a currency pair has an effective timestamp. Support adding rates in any order and querying the rate that was in effect for a pair at a given time.
Stripe settles charges in many presentment and settlement currencies, and refunds or disputes must be converted with the rate that applied when the original charge happened, not today's rate. The interviewer checks that you choose a sorted structure per pair, use binary search correctly for a predecessor query and avoid floating-point money.
What the Interviewer Expects
- Partition data by currency pair so each query touches one sorted list
- Use a predecessor search (`bisect_right` minus one) to find the latest rate at or before the timestamp
- Return a clear result (None) when no rate existed yet
- Keep rates as `Decimal` rather than float
- Decide what happens when the same pair and timestamp are inserted twice
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you make inserts O(log n) instead of O(n) for very long histories?
- How would you derive a rate for a pair you do not store directly, such as via USD?
- How would you serve this from a cache shared across many API servers?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
add(base, quote, effective_ts, rate) records a rate. get(base, quote, ts) returns the rate whose effective_ts is the largest value <= ts, or None if the first rate starts later. Re-adding th...
High-Level Design
Keep a dict from (base, quote) to two parallel sorted lists: timestamps and rates. Insert with bisect_left, overwrite on an exact timestamp match, otherwise insert at that position. Query with `...