Question
Let be a priority queue for maintaining a set of elements. Suppose is implemented using a max-heap data structure. The operation Extract-Max(A) extracts and deletes the maximum element from . The operation Insert(A, key) inserts a new element key in . The properties of a max-heap are preserved at the end of each of these operations.
When contains elements, which one of the following statements about the worst case running time of these two operations is TRUE?
Official answer and solution
- Official answer
- B
- From
- Official GATE 2023 answer key
Solution
Recall standard worst-case time complexities of binary heap operations.
In a max-heap containing elements:
- Extract-Max(A): The root contains the maximum element. Extracting it requires replacing the root with the last leaf and calling
Max-Heapify, which traverses down the height of the tree. The tree height is , so worst-case time is . - Insert(A, key): Inserts the new element at the bottom-most leaf and bubbles it up toward the root to restore the max-heap property. Traversing up the tree height takes in the worst case.
Thus, both operations run in time.
Option (B)
Confusing Find-Max (which is ) with Extract-Max (which deletes the maximum and requires heapify).
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2023 Computer Science and Information Technology (CS)
- Question
- 46 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.