GATE / CS ARCHIVE

GATE CSE 2023 / QUESTION 46

GATE CSE 2023 Q46

Data Structures · Heaps & Priority Queues · Full GATE CSE 2023 paper

GATE 2023, Q46 2 marks · MCQ
Report issue

Heaps & Priority Queues

Question 46, GATE 2023: A priority queue of n elements is implemented as a max-heap. What are the worst-case times for Extract-Max (remove maximum and restore heap) and Insert (add key and restore heap)? A both O(1); B both O(log n); C O(1),O(n); D O(1),O(log n).
Tap to enlarge
Your answer
Answer & solution

Official answer: B

A binary heap has heightΘ(log n). Extract-Max removes the root, moves the final element there, and can sift it down the full height. Insertion appends a leaf and can sift it up the full height. Both therefore take O(log n) worst-case time, option B, under the ordinary heap-storage model without charging an occasional external dynamic-array resize.

Teaching detail. Reading a heap's maximum is O(1) because it is at the root, but Extract-Max includes deletion and restoration, which is more work. A replacement root may be smaller than descendants all the way to a leaf, requiring one comparison/swap step per level. A newly inserted large key may similarly rise from its leaf to the root. A complete binary tree has logarithmic height, giving both upper bounds, and such examples attain that height. Neither operation scans all n items in the ordinary representation.

Original paper, page 49 · Answer key