Maintain a Rolling Time-Window VWAP Over a Live Trade Stream
Last updated: November 29, 2026
Quick Overview
Build a class that ingests trades (timestamp, price, quantity) in time order and returns the volume-weighted average price over the trailing N nanoseconds at any query time, in amortized O(1) per call.
Millennium
November 29, 20260
0
0 solved
Build a class that ingests trades (timestamp, price, quantity) in time order and returns the volume-weighted average price over the trailing N nanoseconds at any query time, in amortized O(1) per call.
Execution and analytics services at a multi-strategy fund compute benchmarks like trailing VWAP for every symbol a pod trades, so this is a natural warm-up in a first technical call. The interviewer checks that you avoid recomputing the window on each query and that you handle the empty window and the eviction boundary cleanly.
What the Interviewer Expects
- Keeps running sums of price times quantity and of quantity instead of rescanning
- Evicts expired trades from the front of a deque on both insert and query
- States the window boundary convention (open or closed at now minus N) explicitly
- Returns a sentinel such as None when the window has no volume
- Discusses floating point drift from repeated subtract and add
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 handle thousands of symbols with very different trade rates?
- What changes if trades can arrive slightly out of order?
- How would you avoid floating point drift over a full trading day?
- How would you expose a time-bucketed VWAP (every 1 second) instead?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Trades arrive in non-decreasing timestamp order. vwap(now) must reflect trades with ts > now - window. Each call should be amortized O(1); memory is bounded by trades inside the window....
High-Level Design
Keep a deque of (ts, price, qty) plus two running totals: pv = sum(price * qty) and vol = sum(qty). On every insert or query, pop expired trades from the left and subtract their contribution. VW...