Track Net Position and Realized PnL From a Fill Stream
Last updated: November 29, 2026
Quick Overview
Given a chronological stream of fills (symbol, side, quantity, price), maintain each symbol's net position, average entry price and realized PnL, correctly handling trades that reduce, close or flip a position from long to short.
Imc
November 29, 20260
0
0 solved
Given a chronological stream of fills (symbol, side, quantity, price), maintain each symbol's net position, average entry price and realized PnL, correctly handling trades that reduce, close or flip a position from long to short.
Every IMC trading system needs an accurate per-instrument position and PnL view that risk limits and traders rely on, so this is a natural first coding screen. The interviewer checks that you get the sign logic right when a fill crosses through zero and that you write clean, testable code quickly.
What the Interviewer Expects
- Uses signed quantities so buys and sells share one code path
- Updates weighted average cost only when adding to a position
- Realizes PnL only on the closed portion of a reducing fill
- Handles a flip by opening the remainder at the fill price
- Walks through a hand example before coding
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 you switch from average cost to FIFO lot accounting, and when would the numbers differ?
- How would you add unrealized PnL given a live mid price feed?
- Prices arrive as floats from one venue and integer ticks from another. How do you avoid drift?
- How would you make this safe if fills for one symbol can arrive on two threads?
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: an ordered list of (symbol, side, qty, price) with side in {'B','S'} and qty > 0. Output per symbol: net position (positive long, negative short), average entry price of the open positi...
High-Level Design
Keep three dictionaries keyed by symbol: pos, avg, realized. Convert each fill into a signed quantity. Two cases:
- Same direction or flat: the fill adds exposure. New average = weighted a...