Quant Memo
Coding/●●●●●

An exponentially weighted price average

Instead of a flat average of the last k ticks, you want an exponentially weighted moving average (EWMA): each new price gets weight alpha, and the running average keeps 1 - alpha of its old value. Design a class that updates in constant time and constant memory and returns the current EWMA after each tick.

e = EWMA(alpha=0.5)
e.update(10)  -> 10.0        # first tick seeds the average
e.update(20)  -> 15.0        # 0.5*20 + 0.5*10
e.update(30)  -> 22.5        # 0.5*30 + 0.5*15
e.update(30)  -> 26.25       # 0.5*30 + 0.5*22.5

Each update must run in O(1)O(1) time and O(1)O(1) memory.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions