GATE / CS ARCHIVE

SUBJECT COLLECTION

Data Structures · Heaps & Priority Queues

33 questions, newest first

GATE CSE has asked 33 questions on Heaps & Priority Queues (Data Structures) from 1991 to 2026, in 26 of the 46 papers; the most recent is in GATE CSE 2026 Set 1. In the last five years it carried 1.1 marks per 100-mark paper on average, against 1.2 since 2003. Since 2014 the questions have been 11 MCQ, 4 NAT and 1 MSQ. 6 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
1 mark · MSQ
Report issue

Leaf indices in a binary heap

Heaps & Priority Queues

Question 23, GATE 2026: A binary minheap contains an odd number n>100 of entries in a 1-indexed array. Which positions are not leaves? A (n+1)/2; B (n−1)/2; C (n−3)/2; D n.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, C

Our answer: (n-1)/2 and (n-3)/2

Givens and goal

A binary minheap contains an odd number n>100 of entries in a 1-indexed array. Which positions are not leaves? A (n+1)/2; B (n−1)/2; C (n−3)/2; D n.

Approach

Use the array child-index formula; heap ordering by key value is irrelevant to the tree shape.

Step-by-step solution

1. For a node at position i, its left child would be at 2i and right child at 2i+1. A node is internal if at least the left child exists, which means 2i≤n.
2. Thus internal positions are 1 through floor(n/2); positions floor(n/2)+1 through n are leaves.
3. Write n=2k+1. Then floor(n/2)=k and the proposed positions become A k+1, B k, C k−1, D 2k+1.
4. Positions k and k−1 lie in the internal range. In particular k has children 2k and 2k+1. Positions k+1 and 2k+1 exceed the internal boundary and have no children.
5. Therefore B and C are not leaves. The condition n>100 ensures k−1 is positive and valid; the minheap property affects stored values, not this index boundary.

Original paper, page 18 · Answer key

1 mark · NAT
Report issue

Computing the height of a binary heap

Heaps & Priority Queues

Question 35, GATE 2025: Height is the largest number of edges on a root-to-leaf path. A Min-Heap stores 32 keys. Under the standard binary-heap convention, what is its height?
Tap to enlarge
Answer & solution

Official answer: 5

Approach: Use the complete-binary-tree shape of a binary heap; key order does not determine its height.
At depth d, a binary tree can contain at most 2d nodes. The completely filled levels from depth 0 through depth 4 contain 1+2+4+8+16=31 nodes. A 32nd node therefore cannot fit in those levels and must occupy depth 5.
A heap is complete: all levels except possibly the last are full, with the last filled from left to right. Hence 32 keys create one node at depth 5 and none deeper. The height, measured in edges, is 5.
Equivalently, an n-node complete binary tree has height floor(log₂ n), and floor(log₂32)=5. Counting occupied levels would give 6, but the question explicitly uses edges. The answer relies on the conventional binary meaning of Min-Heap; arbitrary heap-ordered trees need not have this shape.

Original paper, page 35 · Answer key

2 marks · MCQ
Report issue

Heapify into a max-heap

Heaps & Priority Queues

Question 41, GATE 2024: The array [82,101,90,11,111,75,33,131,44,93] is “heapified”. Which first three entries result? A 82,90,101; B 82,11,93; C 131,11,93; D 131,111,90. The original does not specify min-heap versus max-heap; the official answer uses the usual bottom-up max-heap interpretation.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Step through it (13 steps)

Approach: Under the intended max-heap convention, every parent must be at least as large as its children. Build the heap bottom-up by sifting down internal nodes, using zero-based indices.
1. Start at index 4 (value 111), whose child is 93, so no change. At index 3, swap 11 with its larger child 131. Index 2 contains 90 above children 75 and 33 and needs no change.
2. At index 1, 101 has children 131 and 111. Swap with 131; 101 then sits at index 3 above 11 and 44.
3. At index 0, 82 must move below 131. At its new index 1, move it below 111; then at index 4 move it below 93.
4. The resulting array is [131,111,90,101,93,75,33,11,44,82]. Its first three entries are 131,111,90, matching D.
“Heap” alone also allows a min-heap, whose root would be 11, absent from all options. The question also does not name a particular construction algorithm. D is retained as the official intended answer, not as a uniquely forced result of the literal wording. Existing discrepancy CS-2024-S1-Q41-HEAP-TYPE remains open.

Original paper, page 26 · Answer key

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 12, GATE 2023: Which 1-indexed array is a max-heap? A [23,17,10,6,13,14,1,5,7,12]; B [23,17,14,7,13,10,1,5,6,12]; C [23,17,14,6,13,10,1,5,7,15]; D [23,14,17,1,10,13,16,12,7,5].
Tap to enlarge
Your answer
Answer & solution

Official answer: B

In a one-indexed max-heap, A[i] must be at least both A[2i] and A[2i+1] when those children exist. B satisfies all parent-child inequalities: 23≥17, 14; 17≥7, 13; 14≥10, 1; 7≥5, 6; 13≥12. A fails at 10<14, C at 6<7 and 13<15, and D at 1<12. Answer B.

Teaching detail. A heap is only partially ordered: parent keys dominate child keys, but siblings need not be sorted and the array need not be globally descending. There are only five possible parent positions in this ten-element array. For A, index 3 contains 10 and child index 6 contains 14. For C, parent index 4 contains 6 but child 9 contains 7. For D, parent 4 contains 1 but child 8 contains 12. One violation suffices to reject a candidate; every edge must pass to accept B.

Original paper, page 9 · Answer key

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

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

2 marks · MCQ
Report issue

Heap ordering and conversion complexity

Heaps & Priority Queues

Question 40, GATE 2019: Test max-heap statements: I the smallest element is always at a leaf; II the second largest is always a root child; III a max-heap can be built from a BST in Θ(n); IV a BST can be built from a max-heap in Θ(n). Options A I/II/III, B I/II/IV, C I/III/IV, D II/III/IV.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Our answer: A under distinct-key comparison model

1. Under the intended distinct-key model, a max-heap parent is larger than its children. If the smallest key were internal, one of its children would be smaller still, a contradiction. Hence I holds.
2. The largest key is at the root. If the second largest were below depth one, its nonroot parent would have to be larger, producing a third key above it in rank. Thus it must be one of the root's children and II holds.
3. To establish III, traverse the BST once and copy its n keys into an array, costing Θ(n). Then run bottom-up heap construction, also Θ(n), by sifting down internal nodes. Sorting first is not required.
4. IV is false in the general comparison model. If it were possible, arbitrary distinct keys could be heapified in Θ(n), converted to a BST in Θ(n), and output in sorted order by an inorder traversal in Θ(n). That would contradict the Ω(n log n) comparison-sorting lower bound.
5. Therefore the intended true statements are I, II, III: option A. The qualification about distinct keys is material. With repeated minimum values, a minimum can occur at an internal node as well as a leaf, so the literal word 'always' in I is not valid for unrestricted duplicate-key heaps. Specialized bounded-integer models also cannot be silently substituted for comparison complexity.

Original paper, page 13 · Answer key

2 marks · NAT
Report issue

Counting seven-key min-heaps

Heaps & Priority Queues

Question 46, GATE 2018: How many distinct min-heaps use each key 1,2,3,4,5,6,7 once? Use the standard complete binary heap shape.
Tap to enlarge
Answer & solution

Official answer: 80

Method

The shape is fixed: a root with two three-node subtrees. Minimum key 1 must occupy the root. Select a three-element subset of the remaining six keys for the left subtree; the right gets the complement, giving C(6,3)=20 distinct assignments. Within each subtree its least key must be its root, but its two leaves can be swapped independently. Left and right positions are distinguished in a heap, so do not divide the count by 2 for mirror images.

Solution

The minimum 1 must be the root. The complete seven-node shape has three positions in each subtree. Choose which three of the other six keys go left in C(6, 3)=20 ways. In either three-node subtree its smallest key is the root and its two leaves can be ordered in 2 ways. Thus there are 20*2*2=80 heaps.

Original paper, page 19 · Answer key

2 marks · MCQ
Report issue

Heaps & Priority Queues

Question 37, GATE 2016: An array-stored binary heap has depth d, measured in root-to-farthest-leaf edges. After deleting the item at arbitrary index i, what is the efficient worst-case time for restoring heap order? A: O(1). B: O(d) but not O(1). C: O(2^d) but not O(d). D: O(d·2^d) but not O(2^d).
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Replace the deleted element with the last heap element, reduce the heap size, then sift up if the parent order is violated or otherwise sift down. At most one root-to-leaf path is traversed, length≤d. Some deletions require movement throughΘ(d) levels, so the worst-case bound is O(d) and not O(1), B.

Replacing the deleted slot with the last array element preserves the complete-tree shape and avoids shifting an entire subtree. For a min-heap, compare the replacement with its parent: if it is too small, move it upward; otherwise any violation can only be below it, so move it downward by choosing the smaller child. The max-heap case reverses inequalities. There are at most d edges on the chosen path, with constant work at each, and worst-case inputs require traversal to the root or deepest leaf.

Original paper, page 15 · Answer key

2 marks · NAT
Report issue

Heaps & Priority Queues

Question 34, GATE 2016: A complete binary min-heap contains each integer 1,…,1023 exactly once. Root depth is 0. What is the greatest possible depth of key 9?
Tap to enlarge
Answer & solution

Official answer: 8.0 : 8.0

Our answer: 8

A node at depth d has d strictly smaller ancestors in a heap of distinct keys. Key9 has only eight smaller values, so d≤8, also below the full tree’s maximum depth 9. Place1..9 along a root-to-depth 8 chain and assign remaining keys in a parent-before-child order; all heap inequalities can be satisfied. Thus depth 8 is attainable.

There are 1023=210−1 nodes, so levels 0 through 9 exist. Depth 9 would require nine ancestors smaller than 9, but only eight values qualify. A heap is not a BST: siblings need not be numerically ordered, so the remaining values can be assigned without imposing a search-tree constraint on the chosen chain.

Original paper, page 34 · Answer key

2 marks · MCQ
Report issue

Heaps & Priority Queues

Question 37, GATE 2015: Insert 35 into the max-heap array (indices 1–9) [40,30,20,10,15,16,17,8,4]. A [40,30,20,10,15,16,17,8,4,35]; B [40,35,20,10,30,16,17,8,4,15]; C [40,30,20,10,35,16,17,8,4,15]; D [40,35,20,10,15,16,17,8,4,30].
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (5 steps)

Preserve the complete-tree shape by inserting at the next free index 10, then repair only its ancestor path. Its parent is floor(10/2)=5, holding 15, so swap. At index 5 the parent is 2, holding 30, so swap again. At index 2 the parent is 1, holding 40, so stop. Option C stops too early and violates 30≥35.

Solution: Append35 at index 10. It exceeds its parent15 at index 5, then 30 at index 2, but not 40 at the root. The array becomes 40, 35, 20, 10, 30, 16, 17, 8, 4, 15, option B.

Original paper, page 14 · Answer key

1 mark · MCQ
Report issue

Heaps & Priority Queues

Question 22, GATE 2015: A complete binary tree has both root subtrees already max-heaps. What lower bound describes operations to turn it into a heap? AΩ(logn), BΩ(n), CΩ(nlogn), DΩ(n²). The intended interpretation is worst-case repair work, not a lower bound for every instance.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Our answer: A for worst-case heap repair

Sift the root down, swapping with the larger child until order is restored. The path can have Θ(log n) levels, and a smallest root with heap-ordered descendants realizes that worst case. Thus the intended worst-case lower bound is Ω(log n), A. It is not a per-instance lower bound: a root already exceeding its children needs only constant checking.

Only the root can violate heap order because its subtrees already satisfy it. Compare the root with its larger child and swap downward as needed; each swap moves the offending value one level. A complete n-node tree has height Θ(logn), and placing a sufficiently small value at the root can force a descent to a leaf. That is the intended worst-case lower bound matched by sift-down. If the root already dominates its children, constant work is enough, so the statement cannot mean every instance requires logarithmic work.

Original paper, page 33 · Answer key

1 mark · MCQ
Report issue

Heaps & Priority Queues

Question 30, GATE 2015: An array in level-order heap representation is (89,19,50,17,12,15,2,5,7,11,6,9,100). What is the minimum number of interchanges needed to convert it into a max-heap? An interchange swaps two elements. Options: (A) 4; (B) 5; (C) 2; (D) 3.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

1. With 1-based indices, swaps (13,6), (6,3), (3,1) yield (100,19,89,17,12,50,2,5,7,11,6,9,15). Check each parent against its children: every parent is at least as large. This gives a concrete three-swap witness, not merely the count performed by an unspecified heapify routine.

2. Three swaps along the ancestor chain move 100 through indices 13→6→3→1, producing a valid max-heap. Fewer cannot suffice: 100 must reach the root. If swapped directly with 89, the resulting 89 at 13 exceeds both ancestors 15 at 6 and 50 at 3; one additional swap cannot repair both (swapping with 3 leaves 50 below 15; swapping with 6 leaves 89 below 50). If100 reaches the root in two swaps, at least one of the remaining displaced chain values is still out of heap order. Thus the minimum is 3, D.

Original paper, page 63 · Answer key

1 mark · MCQ
Report issue

Heaps & Priority Queues

Question 12, GATE 2014: A max-heap initially has level order 10,8,5,3,2. Insert 1 and then 7. What is the new level order? A 10,8,7,3,2,1,5; B 10,8,7,2,3,1,5; C 10,8,7,1,2,3,5; D 10,8,7,5,3,2,1.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step through it (5 steps)

1. Set up the reasoning. Represent the heap as a one-based array. Append each item at the first available position to preserve the complete-tree shape, then compare it only with its ancestors while restoring max-heap order.

2. Derive the result. Appending1 puts it below 5 and requires no swap. Appending7 puts it below 5, so exchange 7 and 5; 7 is below root10 and stops. The resulting level order is 10, 8, 7, 3, 2, 1, 5, A.

3. Check the interpretation. Heap insertion does not sort an entire level. Inserting 7 affects the path through its parent 5; it does not exchange the existing siblings 3 and 2. This distinguishes A from other arrays that might still satisfy some heap inequalities.

Original paper, page 32 · Answer key

1 mark · MCQ
Report issue

Recognizing a max-heap

Heaps & Priority Queues

Question 23, GATE 2011: Which drawn binary tree is a max-heap? Below each node is specified as value:(left child,right child), with '-' for a missing child.

(A) 10:(8,6), 8:(4,-), 6:(5,2), 5:(1,-); remaining nodes are leaves
(B) 10:(8,6), 8:(4,5), 6:(1,2); remaining nodes are leaves
(C) 10:(5,6), 5:(4,8), 6:(2,1); remaining nodes are leaves
(D) 5:(2,8), 2:(1,4), 8:(6,10); remaining nodes are leaves
Tap to enlarge
Your answer
Answer & solution

Official answer: B

A binary max-heap must have complete-tree shape as well as parent≥child values. B is complete and satisfies 10≥8, 6; 8≥4, 5; 6≥1, 2. A satisfies value order but not completeness. C has child8 greater than parent5; D also violates value order. Thus B.

Completeness requires every level except possibly the last to be full and the last level to be filled from left to right. In A, node 8 lacks a right child while positions to its right are occupied, and a deeper node exists despite that gap. In B every displayed level is full, and every parent dominates its two children. C fails at 5→8; D fails already at 5→8 and also at 2→4 and 8→10. Checking only the root maximum is insufficient.

Original paper, page 6 · Answer key

2 marks · MCQ
Report issue

Recognize an array max-heap

Heaps & Priority Queues

Question 59, GATE 2009: Which zero-based array is a binary max-heap? A[25, 12, 16, 13, 10, 8, 14]; B[25, 14, 13, 16, 10, 8, 12]; C[25, 14, 16, 13, 10, 8, 12]; D[25, 14, 12, 13, 10, 8, 16].
Question 59, GATE 2009: Which zero-based array is a binary max-heap? A[25, 12, 16, 13, 10, 8, 14]; B[25, 14, 13, 16, 10, 8, 12]; C[25, 14, 16, 13, 10, 8, 12]; D[25, 14, 12, 13, 10, 8, 16].
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

In a zero-based heap each parent at i must be at least as large as children at 2i+1 and 2i+2. In C, [25, 14, 16, 13, 10, 8, 12], the checks are 25≥14, 16; 14≥13, 10; and 16≥8, 12, so every edge satisfies max-heap order. A fails because parent 12 has child 13; B fails because parent 14 has child 16; D fails because parent 12 has child 16. Thus C alone is a max-heap.

A max-heap needs parent≥child along every edge, not a globally descending array. In C, root 25 exceeds 14/16; 14 exceeds 13/10; 16 exceeds 8/12. A fails at 12<13; B at 14<16; D at 12<16. Checking only the root would wrongly admit the distractors; every internal node must satisfy the local condition.

Original paper, page 14

2 marks · MCQ
Report issue

Two delete-max operations

Heaps & Priority Queues

Question 60, GATE 2009: Perform two delete-max operations on the valid heap from Q59. Result? A[14, 13, 12, 10, 8]; B[14, 12, 13, 8, 10]; C[14, 13, 8, 12, 10]; D[14, 13, 12, 8, 10].
Question 60, GATE 2009: Perform two delete-max operations on the valid heap from Q59. Result? A[14, 13, 12, 10, 8]; B[14, 12, 13, 8, 10]; C[14, 13, 8, 12, 10]; D[14, 13, 12, 8, 10].
Question 60, GATE 2009: Perform two delete-max operations on the valid heap from Q59. Result? A[14, 13, 12, 10, 8]; B[14, 12, 13, 8, 10]; C[14, 13, 8, 12, 10]; D[14, 13, 12, 8, 10].
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (9 steps)

Start from Q59’s heap [25, 14, 16, 13, 10, 8, 12]. Delete 25, move final element 12 to the root, and sift down by swapping with the larger child 16. The six-element heap is [16, 14, 12, 13, 10, 8]. Delete 16 and move final element 8 to the root: [8, 14, 12, 13, 10]. Swap 8 with larger child 14, then with larger child 13, giving [14, 13, 12, 8, 10]. This is option D. Each deletion removes the maximum and reduces the active heap length by one.

Delete-max replaces the root by the last active element, shrinks the heap and repeatedly swaps downward with the larger child. First deletion moves 12 to the root and swaps it with 16, yielding[16, 14, 12, 13, 10, 8]. Second deletion moves 8 to the root, then swaps with 14 and 13, yielding[14, 13, 12, 8, 10]. Choosing the smaller child would not restore heap order, which explains several plausible-looking alternatives.

Original paper, page 14

2 marks · MCQ
Report issue

Batch insertion into a binary heap

Heaps & Priority Queues

Question 47, GATE 2008: A binary heap contains n elements. Insert another n elements, not necessarily one at a time. What is the optimum total time?

(A) Θ(log n)
(B) Θ(n)
(C) Θ(n log n)
(D) Θ(n²)
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Append all n new elements to the existing n-element heap array, then run bottom-up BUILD-HEAP on the resulting 2n elements. Its total sift-down work is O(n), since the number of nodes of height h decreases geometrically and Σh/2h is constant.
2. Reading/storing n new elements requires Ω(n), so the optimum total is Θ(n), option B. The permission to insert them in a batch is crucial: n separate ordinary heap insertions can require Θ(n log n).

Original paper, page 8

2 marks · MCQ
Report issue

Binary search along the heap insertion path

Heaps & Priority Queues

Question 47, GATE 2007: Insert an element into an array-represented max heap of n elements. Use binary search along the path from the new leaf to the root to locate its insertion position. What is the asymptotic worst-case number of key comparisons?
Options:
(A) Θ(log₂ n)
(B) Θ(log₂ log₂ n)
(C) Θ(n)
(D) Θ(n log₂ n)
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Approach. The binary search is not across the entire array. It searches the sorted ancestor chain, whose length is logarithmic in n; take the logarithm of that length.

2. Derivation and interpretation. A complete binary heap has a root-to-new-leaf path containing Θ(log n) positions. Existing keys along this path are monotone because it is a max heap. Binary search among h=Θ(log n) positions uses Θ(log h)=Θ(log log n) key comparisons in the usual worst-case interpretation, so B. This is a bound on comparisons, not all insertion work: shifting keys along the path can still require Θ(log n) moves. Array indices allow an ancestor at a specified depth to be selected without doing key comparisons.

Original paper, page 11

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

2 marks · MCQ
Report issue

Valid array representation of ternary max-heap

Heaps & Priority Queues

Question 76, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Which array is a valid heap?
Options:
(A) [1,3,5,6,8,9]
(B) [9,6,3,1,8,5]
(C) [9,3,6,8,5,1]
(D) [9,5,6,8,3,1]
Question 76, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Which array is a valid heap?
Options:
(A) [1,3,5,6,8,9]
(B) [9,6,3,1,8,5]
(C) [9,3,6,8,5,1]
(D) [9,5,6,8,3,1]
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. In a zero-based ternary heap, children of i are 3i+1,3i+2,3i+3. For D=[9,5,6,8,3,1], root 9 dominates 5,6,8 and node 5 dominates children 3,1.

2. A fails at the root; B has parent 6 with child 8; C has parent 3 with child 5. Only D is a valid max-heap.

3. Teaching check: Heap order is a parent-child condition, not full sorting. For six entries only root 0 and node 1 have children; checking these is sufficient. Option D’s 8 may exceed node 1’s 5 because they are siblings, so that comparison imposes no restriction.

Original paper, page 12

2 marks · MCQ
Report issue

Four insertions into ternary heap

Heaps & Priority Queues

Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]
Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]
Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step through it (9 steps)

1. Start with [9,5,6,8,3,1]. Insert 7 at index 6 and exchange with parent index 1, obtaining [9,7,6,8,3,1,5].

2. Insert 2 at index 7, whose parent 6 is already larger: [9,7,6,8,3,1,5,2]. Insert 10 at index 8, exchange with parent index 2 (6), then root 9, obtaining [10,7,9,8,3,1,5,2,6].

3. Insert 4 at index 9 under parent 9, with no exchange. Final array [10,7,9,8,3,1,5,2,6,4], A.

4. Teaching check: The parent index is floor((i−1)/3). After appending 10 at index 8, its parent is index 2, then the root, so two upward swaps occur. Appending 4 at index 9 places it under the current 9 and requires no swap.

Original paper, page 12

2 marks · MCQ
Report issue

Inserting into a max heap

Heaps & Priority Queues

Question 34, GATE 2005: Max-heap level order 10, 8, 5, 3, 2 receives 1 then 7. New level order? A10, 8, 7, 5, 3, 2, 1; B10, 8, 7, 2, 3, 1, 5; C10, 8, 7, 1, 2, 3, 5; D10, 8, 7, 3, 2, 1, 5.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (5 steps)

Append 1 as the sixth element; its parent 5 is larger, so no swap occurs. Append 7 as the seventh element; swap it with parent 5, but stop below root 10. The resulting level order is 10, 8, 7, 3, 2, 1, 5, D.

Appending 1 puts it under parent 5 and requires no upward swap. Appending 7 puts it as the other child of 5;7 exceeds 5, so swap them. Its new parent is 10, which is larger, ending the repair. Other nodes remain where they were, yielding 10, 8, 7, 3, 2, 1, 5. A heap is not a sorted array; sorting the level order would unnecessarily rearrange nodes.

Original paper, page 4

2 marks · MCQ
Report issue

Heap built by successive insertions

Heaps & Priority Queues

Question 37, GATE 2004: Insert 32,15,20,30,12,25,16 one at a time into an empty binary max-heap, using upward restoration after each insertion. Which resulting heap is obtained? The four source diagrams are encoded below as level-order arrays (root first, left-to-right at each level).
Options:
(A) [32,30,25,15,12,20,16]
(B) [32,25,30,12,15,20,16]
(C) [32,30,25,15,12,16,20]
(D) [32,25,30,12,15,16,20]
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step through it (11 steps)

1. Insert32,15,20, then 30 bubbles above 15, giving[32,30,20,15]. Insert12 unchanged.

2. Insert25 bubbles above 20, giving[32,30,25,15,12,20];16 remains under 25. Final level order is 32,30,25,15,12,20,16, diagram A.

3. Teaching check: Insertion order matters: the final heap need not be the one obtained by bottom-up heapifying the array or sorting its values. Inserting 30 swaps it above 15; inserting 25 swaps it above 20. All remaining parent-child inequalities already hold.

Original paper, page 5

1 mark · MCQ
Report issue

Fixed-rank selection from a min heap

Heaps & Priority Queues

Question 23, GATE 2003: A min-heap has n elements, with its smallest at the root. For n≥7, what time is sufficient to find the seventh smallest element, as a function of n? A. Θ(n log n); B. Θ(n); C. Θ(log n); D. Θ(1).
Question 23, GATE 2003: A min-heap has n elements, with its smallest at the root. For n≥7, what time is sufficient to find the seventh smallest element, as a function of n? A. Θ(n log n); B. Θ(n); C. Θ(log n); D. Θ(1).
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. The candidate frontier contains the roots of all unexplored subheaps. Heap order makes its minimum the smallest unseen element. Extract that node and add at most its two children, preserving the invariant. After seven iterations the frontier and work are bounded by constants independent of n.

2. Maintain a small candidate min-priority queue initially containing the root. Repeatedly remove its minimum and insert that node’s children; after seven removals the extracted value is seventh smallest. Only a constant number of nodes and candidate operations are used because rank 7 is fixed, hence Θ(1) with respect to n, D, assuming n≥7. Repeatedly deleting from the original n-element heap is unnecessarily expensive.

Original paper, page 3

1 mark · MCQ
Report issue

Parent index in a one-based binary heap

Heaps & Priority Queues

Question 1.15, GATE 2001: A binary heap occupies array indices 1 through n. For an element at index i, choose the parent index from i−1, floor(i/2), ceil(i/2), and (i+1)/2. Account for the root boundary.
Tap to enlarge
Your answer
Answer & solution

Answer: B for nonroot nodes (no official key was published)

Step 1: A node at index j has left child 2j and right child 2j+1. For an even child index i=2j, division gives i/2=j. For an odd child i=2j+1, floor(i/2)=j.

Step 2: Both cases are captured by floor(i/2), B. For example indices 6 and 7 both have parent 3; ceiling would incorrectly map 7 to 4.

Boundary: The root at i=1 has no parent. The formula returns sentinel index 0, outside the heap, rather than describing a genuine parent.

Original paper, page 2

2 marks · Descriptive
Report issue

Min-heap insertion and root deletion

Heaps & Priority Queues

Question 12(b), GATE 1999: Insert the specified sequence into an initially empty min-heap and draw the final heap and its state after deleting the root. Source discrepancy: GateForum p11 gives 7,6,5,4,3,2,1; GFG p7 visibly gives 7,6,5,4,2,3,1. Both are solved separately below; no official original is available to adjudicate.
Question 12(b), GATE 1999: Insert the specified sequence into an initially empty min-heap and draw the final heap and its state after deleting the root. Source discrepancy: GateForum p11 gives 7,6,5,4,3,2,1; GFG p7 visibly gives 7,6,5,4,2,3,1. Both are solved separately below; no official original is available to adjudicate.
Tap to enlarge
Shared context

Insert the specified sequence into an initially empty min-heap and draw the final heap and its state after deleting the root. Both are solved separately below; no official original is available to adjudicate.

Answer & solution

Answer: [1,4,2,7,5,6,3] → [2,4,3,7,5,6] (no official key was published)

A binary min-heap has complete-tree shape and each parent no larger than either child. Store it in level order. On insertion append at the next free position and swap upward while smaller than its parent. Do not sort the whole array: a heap is only partially ordered.

[7]
[6, 7]
[5, 7, 6]
[4, 5, 6, 7]
[3, 4, 6, 7, 5]
[2, 4, 3, 7, 5, 6]
[1, 4, 2, 7, 5, 6, 3]

The resulting tree is:
             1
          /     \
         4       2
        / \     / \
       7   5   6   3

Delete the root by moving the final element 3 into its position and removing that final slot. This gives [3,4,2,7,5,6]. Swap 3 with its smaller child 2, giving [2,4,3,7,5,6]. Now 3≤6, so downward repair stops. The final tree is:

             2
          /     \
         4       3
        / \     /
       7   5   6

The paper instead orders the last three insertions as 2,3,1. Its successive arrays are:
[7]
[6, 7]
[5, 7, 6]
[4, 5, 6, 7]
[2, 4, 6, 7, 5]
[2, 4, 3, 7, 5, 6]
[1, 4, 2, 7, 5, 6, 3]

Its root-deletion result therefore also coincides. Preserving both source sequences is still important: agreement of the final answer does not establish that the printed questions were identical.

Original paper, page 7

2 marks · MCQ
Report issue

Fewest swaps to make a max-heap

Heaps & Priority Queues

Question 2.11, GATE 1996: For array [89,19,40,17,12,10,2,5,7,11,6,9,70], find the minimum arbitrary pair swaps to obtain a max-heap.
Tap to enlarge
Your answer
Answer & solution

Answer: C: 2 (no official key was published)

Using one-based indices, swap entries 13 and 6, then 6 and 3. The array becomes [89,19,70,17,12,40,2,5,7,11,6,9,10], and every parent is at least as large as its children. For a lower bound, initially 70 at index 13 exceeds parent 10 at index 6. Any one-swap repair must change one of those positions. Moving a sufficiently large value into position 6 either puts 70 below a smaller parent or leaves position 6 larger than its parent 40; moving 70 directly to position 3 leaves 40 at 13 above parent 10. Exhaustive checking of all 78 pairs also finds no one-swap heap. Thus two are necessary and sufficient.

Original paper, page 5

1 mark · Descriptive
Report issue

Identifying the tree structure

Heaps & Priority Queues

Question 14(a), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Identify the data structure.
Question 14(a), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Identify the data structure.
Question 14(a), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Identify the data structure.
Tap to enlarge
Answer & solution

Answer: A binary min-heap. (no official key was published)

Two separate conditions identify this tree. First, its shape is complete: all levels above the last are full and the last level fills from left to right. Second, every parent is no greater than either child: for example 1≤5,18; 5≤7,13; 18≤25,20; and 7≤11,9. The minimum element is therefore at the root, and the tree is a binary min-heap.

It is not a binary search tree. For instance, the root's left subtree contains 5 even though 5>1, and the left child 25 of node 18 exceeds its parent. A heap imposes parent-child order but imposes no sorted order between siblings or across unrelated subtrees. The level-order array in the prompt fully specifies the source figure and provides the customary compact heap representation.

Original paper, page 8

2 marks · Descriptive
Report issue

Deleting key five from the min-heap

Heaps & Priority Queues

Question 14(b), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Show the steps deleting key 5 while retaining the structure.
Question 14(b), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Show the steps deleting key 5 while retaining the structure.
Question 14(b), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Show the steps deleting key 5 while retaining the structure.
Tap to enlarge
Answer & solution

Answer: Replace 5 with last key 27, remove the last leaf, then swap 27 with 7 and with 9. Final array [1,7,18,9,13,25,20,11,27,15,17]. (no official key was published)

Use one-based array indexing. Key 5 is at index 2 and the last node, key 27, is at index 12. Delete the last node and place its value at index 2. Taking the last leaf preserves the complete-tree shape; simply removing node 5 would leave a hole.

After replacement:
[1,27,18,7,13,25,20,11,9,15,17]

The new key is greater than both children 7 and 13. Swap with the smaller child 7 (index 4), not an arbitrary child:
[1,7,18,27,13,25,20,11,9,15,17]

At index 4, children are 11 and 9. Swap 27 with the smaller, 9 (index 9):
[1,7,18,9,13,25,20,11,27,15,17]

Index 9 is a leaf, so stop. Each swap makes the parent of the affected subtree its minimum, and only the downward path can remain out of order. The final tree has levels [1], [7,18], [9,13,25,20], [11,27,15,17]; every parent-child inequality and the complete shape are preserved.

Original paper, page 8

3 marks · Descriptive
Report issue

Deleting any min-heap node

Heaps & Priority Queues

Question 14(c), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Give pseudocode to delete an arbitrary node of an n-node min-heap, with worst-case complexity. Distinguish a supplied node index from a supplied key.
Question 14(c), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Give pseudocode to delete an arbitrary node of an n-node min-heap, with worst-case complexity. Distinguish a supplied node index from a supplied key.
Question 14(c), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Give pseudocode to delete an arbitrary node of an n-node min-heap, with worst-case complexity. Distinguish a supplied node index from a supplied key.
Tap to enlarge
Answer & solution

Answer: Replace the node by the last key and sift up or down as needed. Known-index deletion costs O(log n); locating an arbitrary key can add Θ(n). (no official key was published)

Assume a one-based array H[1..n] and a valid index p. Replacing H[p] by H[n] and shortening the array preserves completeness. The replacement may be smaller than its parent or larger than a child, so a general deletion cannot always use only sift-down.

DELETE_AT(H,n,p):
  require 1 <= p <= n
  if p == n: return n-1
  H[p] := H[n]
  n := n-1
  if p > 1 and H[p] < H[floor(p/2)]:
    while p > 1 and H[p] < H[floor(p/2)]:
      swap H[p], H[floor(p/2)]
      p := floor(p/2)
  else:
    while 2*p <= n:
      c := 2*p
      if c+1 <= n and H[c+1] < H[c]: c := c+1
      if H[p] <= H[c]: break
      swap H[p], H[c]
      p := c
  return n

Only inequalities incident to the replaced node can initially fail. If the replacement is less than its parent, it is also less than the old children, since those children were at least the old value, which was at least the parent. Thus only upward repair is needed. Otherwise the parent relation holds, and downward repair with the smaller child fixes each level without creating a violation above. Equal values need no swap.

A complete binary tree has height floor(log2 n). Either repair traverses at most that many levels and uses O(1) extra space, so a supplied-index deletion is O(log n) worst case. If only a key is supplied and no key-to-index directory exists, a heap is not a search tree: finding the occurrence can cost Θ(n), making the entire deletion Θ(n) worst case. A maintained locator can remove that search cost but must be updated on swaps and handle duplicates explicitly.

Original paper, page 8