GATE / CS ARCHIVE

GATE CSE 2024 SET 1 / QUESTION 43

Locate the largest key in a min-heap

Data Structures · Heaps & Priority Queues · Full GATE CSE 2024 Set 1 paper

GATE 2024 Set 1, Q43 2 marks · MCQ
Report issue

Heaps & Priority Queues

Question 43, GATE 2024: A binary min-heap contains 105 distinct elements. If k is the array index holding the maximum element, how many values can k take over valid heaps? A 53; B 52; C 27; D 1.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Approach: Use the heap-order property to restrict the maximum to leaves, then count leaves in the complete binary-tree shape.
1. In a min-heap every parent is smaller than its children because all keys are distinct. If the maximum were an internal node, its child would need to be still larger, a contradiction. Thus the maximum must be a leaf.
2. With one-based indexing, internal nodes are indices 1 through floor(105/2)=52. Leaves occupy indices 53 through 105, giving 105−52=53 leaves.
3. Every leaf is a feasible maximum location: place the largest key at the chosen leaf and assign the remaining keys in increasing order along a parent-before-child ordering of the remaining nodes. All parent-child inequalities can be satisfied.
Answer: A, 53. Zero-based indexing changes the leaf indices to 52 through 104 but not their number. The maximum is not fixed at the last array position; any leaf can hold it.

Original paper, page 27 · Answer key