About this question

Efficient Simple Moving Average

Easy · Number Theory & Algorithms · Quant Trader interview question · data-structures, algorithms, sma, time-complexity

You are building a high-frequency trading system and need to calculate a Simple Moving Average (SMA) of the last $N$ prices efficiently. The system receives a continuous stream of price data and each new price must update the SMA in $O(1)$ time. Which data structure, along with a running sum, allows for an update time complexity of $O(1)$ for each new price update to the SMA?