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
Coding & Algorithms
Software Engineer
Millennium
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

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
Sliding window
Deque
Running aggregates
Market data
Amortized analysis
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 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 Problems
Sample 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...


Submit Your Answer
Markdown supported

Related Questions