StreakPeaked· Practice

ExamsGATETechnical

Let A be a priority queue for maintaining a set of elements. Suppose A is implemented using a max-heap data structure. The operation EXTRACT-MAX(A) extracts and deletes the maximum element from A. The operation INSERT(A,key) inserts a new element key in A. The properties of a max-heap are preserved at the end of each of these operations. When A contains n elements, which one of the following statements about the worst case running time of these two operations is TRUE?

  1. Both EXTRACT-MAX(A) and INSERT(A,key) run in O(1).
  2. Both EXTRACT-MAX(A) and INSERT(A,key) run in O(log(n)).
  3. EXTRACT-MAX(A) runs in O(1) whereas INSERT(A,key) runs in O(n).
  4. EXTRACT-MAX(A) runs in O(1) whereas INSERT(A,key) runs in O(log(n)).

Correct answer: Both EXTRACT-MAX(A) and INSERT(A,key) run in O(log(n)).

Solution

Both operations, EXTRACT-MAX and INSERT, involve maintaining the heap property, which requires traversing the height of the heap. Since a max-heap is a complete binary tree, the height is logarithmic in relation to the number of elements, leading to a worst-case time complexity of O(log(n)) for both operations.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →