Quant Memo
Coding/●●●●●

Rolling maximum of the last k ticks

Ticks stream in one at a time. After each new price, report the maximum price over the last k ticks (or over all seen so far if fewer than k have arrived).

m = RollingMax(k=3)
m.update(4)  -> 4
m.update(2)  -> 4
m.update(5)  -> 5
m.update(1)  -> 5      # window [2, 5, 1]
m.update(1)  -> 5      # window [5, 1, 1]
m.update(3)  -> 3      # window [1, 1, 3]

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

Your answer

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

More Coding questions