Compute FIFO Realized PnL Across Position Flips From Fills

Last updated: November 29, 2026

Quick Overview

Given a sequence of buy and sell fills for one instrument, compute realized PnL using FIFO lot matching, the final signed position, and the remaining open lots, correctly handling fills that flip the position from long to short or back.

Millennium
Coding & Algorithms
Software Engineer
Millennium
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a sequence of buy and sell fills for one instrument, compute realized PnL using FIFO lot matching, the final signed position, and the remaining open lots, correctly handling fills that flip the position from long to short or back.

Every pod at Millennium is judged on its own PnL, and the firm's position and PnL services must agree with the books to the cent. This question checks careful state handling: lot matching, sign conventions and the flip case that naive solutions get wrong.

What the Interviewer Expects
  • Represents open lots as a queue that always shares one sign
  • Matches against the oldest lot first and splits partial lots
  • Handles a single fill that closes the whole position and opens the opposite side
  • Gets the PnL sign right for closing shorts
  • Traces an example by hand and states complexity
Key Topics to Cover
FIFO lot matching
Position accounting
Deque
Realized vs unrealized PnL
Sign handling
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would average-cost accounting change the answer and the code?
  • How would you add fees and per-share commissions?
  • How do you compute unrealized PnL against a mark price?
  • How would you make this correct when fills are amended or busted after the fact?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list of (side, qty, price) with side B or S. Output: realized PnL, signed position, open lots. FIFO: a closing fill consumes the oldest open lot first. One fill can close existing lots and op...

High-Level Design

Hold a deque of [signed_qty, price] lots. Invariant: all lots have the same sign as the position. For each fill, while the fill's sign is opposite to the front lot, match min(|fill|, |lot|), book ...


Submit Your Answer
Markdown supported

Related Questions