GateAI
Binary Heaps

GATE CSE 2023 · Q46

MCQ2 marksEasy

Time

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

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

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

Binary Heaps

In a binary max-heap containing nn elements:

  1. EXTRACT-MAX(A)\text{EXTRACT-MAX}(A):

    • The maximum element is always at the root (A[1]A[1]).

    • To delete it and maintain the max-heap property, we replace the root with the last element of the heap and decrease the heap size by 1.

    • Then, we call MAX-HEAPIFY on the root, which traverses down the height of the tree in the worst case.

    • Since the height of a binary heap with nn elements is Θ(log⁡n)\Theta(\log n), the worst-case running time of EXTRACT-MAX\text{EXTRACT-MAX} is O(log⁡n)O(\log n).

  2. INSERT(A,key)\text{INSERT}(A, key):

    • The new element is placed at the end of the heap (the next available leaf node).

    • Then, it bubbles up along the path to the root until the max-heap property is satisfied.

    • The maximum distance it can travel is the height of the heap, which is Θ(log⁡n)\Theta(\log n).

    • Thus, the worst-case running time of INSERT\text{INSERT} is O(log⁡n)O(\log n).

Therefore, both operations run in O(log⁡(n))O(\log(n)) in the worst case. Option (B) is correct.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Binary Heaps previous year question is Q46 of the GATE CSE 2023 paper. Find more GATE CSE PYQs on Binary Heaps in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Binary Heaps PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Binary Heaps in GATE CSE

Binary Heaps has 15 GATE CSE PYQs across 13 papers from 2011 to 2026. 5 of them are 2-mark MCQs like this one, and GATE CSE 2023 has 2.

How GATE tests Binary Heaps

More from GATE CSE 2023

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2023 · Q46 · MCQ

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?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.