I was practicing this question for an Interview .
Best data structure that will implement the following 3 operation in minimum time::
a.) insertion.
b.) removing the oldest element.
c.) printing the largest element.
The Best that, i can think of is a Min/Max Heap or a priority Queue. For operation (a) and (c) ,Heap is efficient but, I am not sure, the second operation ‘removing the oldest element’ can be done efficiently using Heap.
So Suggest an ideal data Structure that will implement all 3 operation efficiently.
Thanks!
What about using some kind of max heap for operations a,c and just an additional queue of nodes, storing them in historical order ? If you use Fibonacci Heap, you’ll get O(1) time for operations a and c, and O(logN) time for b. You just need to push pointers to new nodes into queue when adding new element, and delete the node pointed by queue front when deleting oldest element (with poping it from queue of course).