Pregunta de entrevista de Amazon

Explain the data structure you would use to implement pop() and push(Object, int) for a Priority Queue.

Respuestas de entrevistas

Anónimo

15 feb 2011

No ... I did mention it in my review. Interviewed and No offer.

2

Anónimo

21 ene 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

Anónimo

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.

Anónimo

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.

Anónimo

28 ene 2011

I had said red black tree. He seemed to agree

Anónimo

2 feb 2011

Would a heap not be more appropriate?

Anónimo

14 feb 2011

Did you get the job?

1