Calculate total realized capital gains from stock trades using FIFO cost basis

Quick Overview

Given a chronological list of stock buy and sell trades, compute the total realized capital gain or loss using FIFO cost basis. The problem tests queue-based lot matching, splitting one sale across several purchase lots, and keeping the running total in a wide enough integer.

Calculate total realized capital gains from stock trades using FIFO cost basis

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are building a capital gains CLI tax calculator. You are given a chronological list of stock `transactions`, where each transaction is represented as `[type, shares, price]`. The `type` is either `"BUY"` or `"SELL"`, `shares` is the number of shares involved, and `price` is the price per share for that transaction. Compute the total realized capital gain (or loss) using the FIFO (first-in, first-out) cost basis accounting method. When shares are sold, they are matched against the oldest unsold purchased shares first. The capital gain for a sale of `k` shares at price `p` matched against a lot purchased at price `c` is `k * (p - c)`. Return the total realized capital gain as an integer, summed across all `"SELL"` transactions. The result may be negative if losses exceed gains. ### Function Signature ```python def capital_gains(transactions: list[list[str]]) -> int: ``` ### Rules - Each transaction is a list of three strings. `shares` and `price` are written as decimal integers, for example `["BUY", "10", "100"]`. - One sale can draw shares from several lots, and one lot can be split across several sales. A partly sold lot keeps its original purchase price for the shares it still holds. - Shares still held after the last transaction are unrealized and add nothing to the result. ### Constraints - `1 <= transactions.length <= 10⁴` - Each transaction is `[type, shares, price]` where `type` is `"BUY"` or `"SELL"`. - `1 <= shares <= 10⁶` - `1 <= price <= 10⁶` - Transactions are listed in chronological order. - Total shares sold never exceed total shares held at any point. ### Examples ```text Input: transactions = [["BUY","10","100"],["SELL","5","120"],["SELL","5","90"]] Output: 50 ``` The first sale gains `5 * (120 - 100) = 100`. The second sale matches the same lot and gives `5 * (90 - 100) = -50`, so the total is 50. ```text Input: transactions = [["BUY","100","50"],["BUY","50","60"],["SELL","120","70"]] Output: 2200 ``` FIFO takes all 100 shares of the first lot (`100 * (70 - 50) = 2000`) and then 20 shares of the second lot (`20 * (70 - 60) = 200`). The 30 shares left in the second lot are never sold, so they add nothing. ```text Input: transactions = [["BUY","5","10"],["BUY","5","20"],["SELL","7","15"],["BUY","3","5"],["SELL","4","8"]] Output: -18 ``` The first sale uses the 5 shares bought at 10 (`+25`) and 2 of the shares bought at 20 (`-10`). The second sale uses the 3 remaining shares bought at 20 (`-36`) and 1 share bought at 5 (`+3`), for a total of `25 - 10 - 36 + 3 = -18`.

Overview: Given a chronological list of stock buy and sell trades, compute the total realized capital gain or loss using FIFO cost basis. The problem tests queue-based lot matching, splitting one sale across several purchase lots, and keeping the running total in a wide enough integer.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are building a capital gains CLI tax calculator. You are given a chronological list of stock transactions, where each transaction is represented as [type, shares, price]. The type is either "BUY" or "SELL", shares is the number of shares involved, and price is the price per share for that transaction.

Compute the total realized capital gain (or loss) using the FIFO (first-in, first-out) cost basis accounting method. When shares are sold, they are matched against the oldest unsold purchased shares first. The capital gain for a sale of k shares at price p matched against a lot purchased at price c is k * (p - c).

Return the total realized capital gain as an integer, summed across all "SELL" transactions. The result may be negative if losses exceed gains.

Function Signature

def capital_gains(transactions: list[list[str]]) -> int:

Rules

  • Each transaction is a list of three strings. shares and price are written as decimal integers, for example ["BUY", "10", "100"] .
  • One sale can draw shares from several lots, and one lot can be split across several sales. A partly sold lot keeps its original purchase price for the shares it still holds.
  • Shares still held after the last transaction are unrealized and add nothing to the result.

Constraints

  • 1 <= transactions.length <= 10⁴
  • Each transaction is [type, shares, price] where type is "BUY" or "SELL" .
  • 1 <= shares <= 10⁶
  • 1 <= price <= 10⁶
  • Transactions are listed in chronological order.
  • Total shares sold never exceed total shares held at any point.

Examples

Input: transactions = [["BUY","10","100"],["SELL","5","120"],["SELL","5","90"]]
Output: 50

The first sale gains 5 * (120 - 100) = 100. The second sale matches the same lot and gives 5 * (90 - 100) = -50, so the total is 50.

Input: transactions = [["BUY","100","50"],["BUY","50","60"],["SELL","120","70"]]
Output: 2200

FIFO takes all 100 shares of the first lot (100 * (70 - 50) = 2000) and then 20 shares of the second lot (20 * (70 - 60) = 200). The 30 shares left in the second lot are never sold, so they add nothing.

Input: transactions = [["BUY","5","10"],["BUY","5","20"],["SELL","7","15"],["BUY","3","5"],["SELL","4","8"]]
Output: -18

The first sale uses the 5 shares bought at 10 (+25) and 2 of the shares bought at 20 (-10). The second sale uses the 3 remaining shares bought at 20 (-36) and 1 share bought at 5 (+3), for a total of 25 - 10 - 36 + 3 = -18.