In this earlier question, the OP asked for a data structure similar to a stack supporting the following operations in O(1) time each:
- Push, which adds a new element atop the stack,
- Pop, which removes the top element from the stack,
- Find-Max, which returns (but does not remove) the largest element of the stack, and
- Find-Min, which returns (but does not remove) the smallest element of the stack.
A few minutes ago I found this related question asking for a clarification on a similar data structure that instead of allowing for the max and min to be queried, allows for the median element of the stack to be queried. These two data structures seem to be a special case of a more general data structure supporting the following operations:
- Push, which pushes an element atop the stack,
- Pop, which pops the top of the stack, and
- Find-Kth, which for a fixed k determined when the structure is created, returns the kth largest element of the stack.
It is possible to support all of these operations by storing a stack and an balanced binary search tree holding the top k elements, which would enable all these operations to run in O(log k) time. My question is this: is it possible to implement the above data structure faster than this? That is, could we get O(1) for all three operations? Or perhaps O(1) for push and pop and O(log k) for the order statistic lookup?