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
November 29, 20260
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
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 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 ProblemsSample 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 ...