FIFO Lot Matching for Realized and Unrealized Trading PnL
Last updated: November 29, 2026
Quick Overview
Given a time-ordered list of fills (symbol, signed quantity, price) and end-of-day mark prices, compute each symbol's position, realized PnL using FIFO lot matching, and unrealized PnL against the mark.
Virtu Financial
November 29, 20260
0
0 solved
Given a time-ordered list of fills (symbol, signed quantity, price) and end-of-day mark prices, compute each symbol's position, realized PnL using FIFO lot matching, and unrealized PnL against the mark.
Virtu's market makers flip between long and short many times a day, and the risk and accounting systems must attribute PnL correctly through those flips. The assessment checks lot bookkeeping, sign handling when a fill both closes and opens a position, and clean separation of realized and unrealized PnL.
What the Interviewer Expects
- Keep a FIFO queue of open lots per symbol
- Close lots only against fills of the opposite sign
- Handle a fill that closes the whole position and opens a new one in the other direction
- Compute unrealized PnL from remaining lots and the mark
- Explain why FIFO, LIFO, and average cost give the same total but different splits
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 code?
- How would you include fees, rebates, and per-share commissions?
- How would you make this incremental for intraday risk?
- How would you handle a trade bust that cancels an earlier fill?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Positive quantity is a buy, negative a sell. Realized PnL for closing c units of a lot opened at p0 at price p is c*(p - p0) for a long lot and c*(p0 - p) for a short lot. Unrealized PnL is ...
High-Level Design
Per symbol a deque of [signed_qty, price] lots, all of the same sign. A fill first eats lots from the front while signs are opposite, then any leftover quantity becomes a new lot. Total PnL (realize...