GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Find opposite heap extreme among leaves

4 questions solved the same way, newest first. Back to Data Structures

How these are solved

Heap order confines a possible opposite extreme to leaves; enumerate floor(n/2)+1 through n in one-based indexing, then count candidates or minimum comparisons. 2006 asks min in maxheap, others max in minheap; index count and comparison count are different outputs.

2 marks · MCQ
Report issue

Locate the largest key in a min-heap

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

1 mark · MCQ
Report issue

Heaps & Priority Queues

Question 2, GATE 2021: A binary min-heap of n elements is stored as an array. Find optimal worst-case time to find its maximum. A Θ(1); B Θ(log n); C Θ(n); D Θ(n log n).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

A maximum occurs among the heap leaves, of which there are ceil(n/2). Scan those leaves to find it in O(n). Their values have no heap-imposed relative order, so in the worst case each must be considered, yielding Ω(n). Hence Θ(n), C.

In a one-based heap array, indices floor(n/2)+1 through n are leaves. Initialize best to the first leaf and compare each remaining leaf once. Heap order says ancestors are no larger than descendants, so inspecting internal nodes is unnecessary. An adversary can assign distinct arbitrary values to leaves above all internal values; finding their maximum requires leaf-count−1 comparisons.

Original paper, page 55 · Answer key

2 marks · NAT
Report issue

Heaps & Priority Queues

Question 47, GATE 2020: An array-represented binary min-heap contains 1023 elements. What is the minimum guaranteed number of comparisons needed to find a maximum?
Tap to enlarge
Answer & solution

Official answer: 511

Worked explanation

In a one-based heap array, node i has children 2i and 2i+1 when present. Therefore internal nodes are 1…floor(1023/2)=511 and leaves are 512…1023, totaling 512. Every internal value is at most a descendant leaf value by repeatedly applying the min-heap property, so a maximum exists among leaves. There is no ordering constraint between distinct leaves. In the worst case each of 511 nonwinning leaf candidates must lose a comparison to eliminate it; one comparison can eliminate at most one candidate. A scan maintaining the largest leaf seen uses exactly 511 comparisons and attains that bound.

Derivation and answer

The1023-node complete binary min-heap has 512 leaves, at array positions 512–1023 under one-based indexing. A maximum can be chosen among the leaves; their relative values are otherwise unconstrained by the heap property. Finding the maximum of 512 candidates requires 511 comparisons in the worst case, achievable by a linear scan. This is the optimal guaranteed comparison count, not a claim about every favorable tied instance.

Original paper, page 14 · Answer key

1 mark · MCQ
Report issue

Finding the minimum in a max heap

Heaps & Priority Queues

Question 10, GATE 2006: What is the worst-case asymptotic time for finding the smallest of n numbers stored in a binary max-heap?
Options:
(A) Θ(n)
(B) Θ(log n)
(C) Θ(log log n)
(D) Θ(1)
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. A minimum occurs among the leaves. There are Θ(n) leaves and their values have no guaranteed mutual ordering.

2. Scanning them gives O(n) time; arbitrary leaf values require Ω(n) comparisons in the worst case. Therefore Θ(n), A.

3. Teaching check: A max-heap constrains each parent to be at least its children, so following children can only decrease values. Every internal node has some leaf descendant no larger than itself. However the roughly n/2 leaves have no ordering among themselves, so binary-search reasoning does not apply.

Original paper, page 2