Compute Realized Gains Using FIFO Cost Basis Lots
Last updated: November 29, 2026
Quick Overview
Given a time-ordered list of buys and sells for one asset, compute the realized gain or loss of each sell using first-in, first-out cost basis. Reject any sell that exceeds the current holdings.
Coinbase
November 29, 20260
0
0 solved
Given a time-ordered list of buys and sells for one asset, compute the realized gain or loss of each sell using first-in, first-out cost basis. Reject any sell that exceeds the current holdings.
Coinbase generates gain and loss reports for customers' taxes, so cost basis logic is a natural phone screen problem. The interviewer wants clean lot tracking with a queue, careful partial lot consumption and a clear answer on what happens with bad input.
What the Interviewer Expects
- Model each buy as a lot with remaining quantity and unit cost
- Consume lots from the oldest first and split a lot when a sell is partial
- Validate oversells before mutating state
- Use integer or Decimal amounts, not floats, for money
- Explain how the design extends to LIFO or specific-lot identification
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 support HIFO (highest cost first) to minimize taxes?
- How do transfers between a user's own wallets affect lots?
- How would you handle fees on buys and sells?
- How would you split gains into short-term and long-term holding periods?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: events (kind, qty, price) in time order for one asset. Output: one realized gain per sell, computed as proceeds minus FIFO cost. A sell larger than holdings is an error and must not partially...
High-Level Design
Keep a deque of open lots [remaining_qty, unit_cost]. A buy appends a lot. A sell first checks total holdings, then repeatedly takes from the front lot, adding used * (sell_price - lot_cost) to th...