Quant Memo
Coding/●●●●●

Design a stack that reports its range

You are buffering the ticks currently in a short window and want a cheap volatility gauge: the range, highest tick minus lowest tick, of whatever is in the buffer right now. As ticks are pushed and popped, get_range must stay O(1)O(1).

s = RangeStack()
s.push(4); s.push(9); s.push(4); s.push(1)
s.get_range() -> 8        # max 9, min 1
s.pop()                   # removes 1
s.get_range() -> 5        # max 9, min 4

Design push, pop, top, and get_range with every operation in O(1)O(1).

Your answer

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

More Coding questions