Explain the data structure you would use to implement pop() and push(Object, int) for a Priority Queue.
Interview Answers
Anonymous
15 Feb 2011
No ... I did mention it in my review. Interviewed and No offer.
2
Anonymous
21 Jan 2015
Heap is the one to choose. Priority queue always pops out the highest valued element(the highest value is defined using weak ordering criterion), pop_heap will make sure the second highest element is placed on top after the first one is popped out, push_heap will also make sure the highest value is placed on top of the queue.
In many cases, this is all you want - the highest valued element, which is what priority queue is operating on..
1
Anonymous
24 Jul 2018
I would use a linkedlist here. I will maintain the linkedlist in a descending order/ The add() method will intake a node and it loops from root and postions in appropiate position O(n)
Peek or poll returns the root element.
Anonymous
23 Mar 2011
I got this question as well. However, the answer is impossible in constant time. A self balancing tree is one way to implement this. In that scenario you have O(log n) time for insertion and removal. You can always have the get minimun in constant time. A van Emde Boas tree or Fusion Tree can speed up this time, but not bring it to constant time.