Sliding Window Problems Applied to Time Series
Sliding window interview questions hinge on whether the window is a tick count or a time span, and each version needs a different structure.
Sliding window problems in trading interviews replace the abstract array of the textbook version with a stream of ticks, and the interviewer's real interest is in whether the window is defined by a fixed count of observations or a fixed span of time, since the two versions need different structures. Four variants recur most: a rolling average or sum over the last N ticks, the running maximum over a window for a breakout signal, the longest stretch where price stays inside a band, and a volume-weighted average price computed over a time span rather than a tick count.
State the problem precisely: given a stream of prices, report the sum or average of the most recent N prices after every new tick arrives. A fixed-size circular buffer or deque holding exactly N values, together with a running total updated by adding the new value and subtracting the value that falls out of the window, answers every query in O(1) rather than resumming the window from scratch, which would cost O(N) per tick.
The follow-up asks what happens once N itself needs to change dynamically, for instance switching from a one-minute to a five-minute lookback in response to a volatility regime change. A circular buffer sized for the smaller window has to be resized or replaced, and the candidate is expected to propose either overallocating the buffer for the largest N the strategy will ever use, or maintaining several running sums at different horizons in parallel rather than trying to resize a live structure under load.
State the problem precisely: report the maximum price over the last N ticks after every new tick, which a breakout strategy at a firm like Tower Research Capital checks against the current price to decide whether a new high has been set. A monotonic deque, which stores indices in decreasing order of their price and discards from the back any index whose price is smaller than the incoming one before it is ever considered, keeps the front of the deque as the current maximum, and every tick is pushed and popped from the deque at most once, giving amortised O(1) per tick rather than O(N) from rescanning the window.
The follow-up asks for the running maximum and the running minimum simultaneously, which a candidate who only implemented one monotonic deque has to generalize by running two deques in parallel, one increasing and one decreasing, rather than trying to force a single structure to answer both queries at once. A second follow-up asks how the deque behaves when many ticks in a row carry the identical price, and the expected answer is that ties are handled cleanly as long as the eviction rule uses a strict rather than a non-strict comparison, which the candidate should state rather than leave implicit.