Rolling Per-Symbol VWAP: Fixed Window, Variable Window and Concurrent Reads
Company: DRW
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Technical Screen
Trades arrive one at a time as objects with four fields: `symbol`, `price`, `qty` and `timestamp`. Build a component that reports the rolling volume-weighted average price (VWAP) of a ticker over a sliding time window. Over the trades of that symbol whose timestamps fall inside the window, $\text{VWAP} = \frac{\sum \text{price} \times \text{qty}}{\sum \text{qty}}$. The interview had several follow-ups, and each part below extends the previous one.
```python
from dataclasses import dataclass
@dataclass(frozen=True)
class Trade:
symbol: str
price: float
qty: int
timestamp: float # seconds
```
Example used throughout, with a 60-second window, for symbol `AAPL`: a trade of 10 shares at price 100 at time 0, 30 shares at 102 at time 30, and 20 shares at 101 at time 70. At time 30 the VWAP is (100 × 10 + 102 × 30) / 40 = 101.5. At time 70 the first trade has left the window, so the VWAP is (102 × 30 + 101 × 20) / 50 = 101.6.
### Clarifying Questions
- Do trades arrive in timestamp order, per symbol or across all symbols, and what should happen to a late trade?
- Is the window measured back from the query time or from the symbol's latest trade, and does a trade exactly one window old count?
- What should the VWAP be when the window holds no trades?
- Are prices floating-point numbers or integer ticks, and how much precision is needed?
- Roughly how many symbols and trades per second are there, and how long must history be kept?
### Part 1 — Fixed window
```python
class RollingVWAP:
def __init__(self, window: float): ...
def add_trade(self, trade: Trade) -> None: ...
def get_vwap(self, symbol: str, now: float) -> float | None: ...
```
The window length is fixed at construction. Both operations should stay fast however many trades the window holds.
```hint Two running totals
The VWAP depends on only two quantities per symbol. Think about how each changes when a trade enters the window and when one leaves it.
```
#### What This Part Should Cover
- Per-symbol state and how expired trades are removed
- The cost of updates and queries, and memory bounded by the window
- The empty window and the boundary rule
### Part 2 — Variable window length
The caller now chooses the window length on every query, `get_vwap(symbol, window, now)`, and different queries may use different lengths.
```hint Expiry no longer fits
A trade too old for one query may be needed by the next, so it cannot be dropped on arrival. Consider what you could precompute over a symbol's time-ordered trades so that the totals over any time range are cheap to get.
```
#### What This Part Should Cover
- Keeping each symbol's trades ordered by time together with cumulative data
- Locating the window's start quickly, and the resulting query cost
- Memory growth, and how to bound it if a maximum window length exists
### Part 3 — Concurrent reads
Several threads now read VWAPs while trades keep being written. Make the component thread-safe without making unrelated symbols wait on each other more than necessary.
```hint Name the shared state
List exactly what a writer changes and what a reader reads, and which of those values must be seen together for one VWAP to be consistent.
```
#### What This Part Should Cover
- The critical sections on the write and read paths
- Lock granularity, such as one lock per symbol, and safe creation of a new symbol's state
- Why Python's global interpreter lock does not make multi-step operations atomic, and options for read-heavy workloads
### What a Strong Answer Covers
- Correct VWAP arithmetic checked against the example, with no division by zero
- Clear costs for adding trades and answering queries in each part
- Numerical care with running sums of floating-point values
- A clean API and explicit assumptions about ordering, boundaries and empty windows
- A race-free concurrent design with small critical sections
### Follow-up Questions
- Trades can arrive late and out of order. How would you change each part?
- History in Part 2 grows without bound. How would you trim it while keeping the range lookups correct?
- After millions of additions and subtractions, a floating-point running sum drifts. How would you prevent that?
- Clients want every symbol's VWAP pushed to them once per second. What changes?
Overview: Build a rolling volume-weighted average price (VWAP) tracker that ingests trades with symbol, price, quantity and timestamp and reports a ticker's VWAP over a sliding time window. Follow-ups let each query choose its own window length and add concurrent readers, testing per-symbol state, query complexity, numerical care and thread safety.
Read the full DRW Software Engineer interview experience this question came from