Amazon Interview Question
SDE1sCountry: United States
It should be correct. This is what the javadoc or PriorityQueue class says:
/**An unbounded priority {@linkplain Queue queue} based on a priority heap.
* The elements of the priority queue are ordered according to their
* {@linkplain Comparable natural ordering}, or by a {@link Comparator}
* provided at queue construction time, depending on which constructor is
* used. A priority queue does not permit {@code null} elements.
* A priority queue relying on natural ordering also does not permit
* insertion of non-comparable objects (doing so may result in {@code ClassCastException}).*/
Priority queue represented as a balanced binary heap: the two children of queue[n] are queue[2*n+1] and queue[2*(n+1)]. The priority queue is ordered by comparator, or by the elements' natural ordering, if comparator is null: For each node n in the heap and each descendant d of n, n <= d. The element with the lowest value is in queue[0], assuming the queue is nonempty.
Why did the interviewer say you're wrong?
I guess you said "min-heap" and in fact it can be either a MIN-heap or MAX-heap or some order sorting order it all depends on your Comparator compareTo function implementation. But in general terms it's modelled internally as a Heap.
- guilhebl April 30, 2017