Skip to content

Calculate PnL for Stock Transactions

Level: L3-L4 Topics: Queues (FIFO), Hash Maps, Financial Modeling

Problem Statement

You are given a list of stock transactions for a single year. Each transaction contains:

  • Symbol -- the stock ticker (e.g., "GOOG", "AAPL")
  • Action -- BUY or SELL
  • Quantity -- number of shares
  • Price -- price per share

The transactions may involve multiple symbols and arrive in any order (not necessarily sorted by time).

When selling shares, lots are sold using FIFO (First In, First Out) order: the earliest purchased shares are sold first.

Calculate the total realized profit and loss (PnL) for the year across all symbols.

Background & Constraints

  • A SELL can only occur if enough shares have been previously purchased for that symbol.
  • A single SELL may consume shares from multiple BUY lots (if the sell quantity exceeds a single lot).
  • You do not need to track unrealized PnL (unsold shares are ignored).
  • Assume the input is valid: no selling more shares than owned.

Examples

Example 1:

Transactions:
  BUY  GOOG 100 @ $5.00
  BUY  GOOG 100 @ $6.00
  SELL GOOG  50 @ $7.00

PnL Calculation (FIFO):
  Sell 50 shares @ $7.00 = $350.00 revenue
  These 50 shares come from the first lot (bought @ $5.00):
    Cost = 50 × $5.00 = $250.00

  PnL = $350.00 - $250.00 = $100.00

Example 2:

Transactions:
  BUY  GOOG 100 @ $5.00
  BUY  GOOG 100 @ $6.00
  SELL GOOG 150 @ $7.00

PnL Calculation (FIFO):
  Sell 150 shares @ $7.00 = $1,050.00 revenue
  First 100 shares from lot 1 (@ $5.00):  cost = $500.00
  Next  50 shares from lot 2 (@ $6.00):   cost = $300.00
  Total cost = $800.00

  PnL = $1,050.00 - $800.00 = $250.00

Example 3 (Multiple symbols):

Transactions:
  BUY  GOOG 100 @ $5.00
  BUY  AAPL  50 @ $10.00
  SELL GOOG 100 @ $4.00    (loss)
  SELL AAPL  50 @ $15.00   (gain)

  GOOG PnL = (100 × $4.00) - (100 × $5.00) = -$100.00
  AAPL PnL = (50 × $15.00) - (50 × $10.00) = $250.00

  Total PnL = -$100.00 + $250.00 = $150.00

Hints & Common Pitfalls

  1. Use a queue per symbol. Maintain a hash map from symbol to a queue (FIFO) of (quantity, price) tuples representing buy lots.

  2. Processing a SELL: Dequeue from the front of the symbol's queue. If the front lot has more shares than needed, partially consume it (reduce its quantity, keep it at the front). If the front lot has fewer shares than the sell quantity, fully consume it and move to the next lot.

  3. Accumulate PnL incrementally. For each sell transaction, compute revenue - cost as you consume lots, and add it to a running total.

  4. Common mistake: Not handling partial lot consumption correctly. When a sell consumes only part of a lot, the remaining shares in that lot must stay at the front of the queue with the original purchase price.

  5. Time complexity: O(T) where T is the total number of transactions, assuming each share is bought once and sold at most once.

Follow-Up Questions

  1. How would the solution change if you use average cost basis instead of FIFO? In average cost, all shares of the same symbol are treated as having the same cost (the running average purchase price). How does this simplify or complicate the data structure?

  2. If the input comes as raw text (e.g., CSV or log lines), how would you parse it? What error checking would you add (invalid symbols, negative quantities, selling more than owned)?

  3. How would you extend this to also report unrealized PnL given a set of current market prices?

  4. If transactions have timestamps and you need PnL as of a specific date, how would you modify the solution?