ACompressed recap
🧾
The k-th largest key of a max-heap (distinct keys) lies at depth ≤ k − 1; the minimum is a leaf. The number of heaps is H(n) = C(n − 1, L)·H(L)·H(R) with L fixed by the shape (1, 1, 2, 3, 8, 20, 80). Insertion needs ≤ h comparisons, deletion ≤ 2h. A d-ary heap has children d·i + 1 … d·i + d and parent ⌊(i − 1)/d⌋.
BMerged rule card
5.4 Heap GATE drills: k-th largest, number of heaps, comparisons, d-ary heaps
- k-th largest (distinct keys) at depth ≤ k − 1; 2nd largest at index 1 or 2; k-th found in O(k log n).
- H(n) = C(n − 1, L)·H(L)·H(n − 1 − L): 1, 1, 2, 3, 8, 20, 80.
- Insert ≤ h, delete ≤ 2h comparisons; find-min O(n).
- d-ary (0-based): children d·i + 1 … d·i + d; parent ⌊(i − 1)/d⌋; height ≈ log_d n.
CInterleaved GATE-style questions
Q1NAT1 markCount
How many distinct max-heaps on 4 distinct keys?
Numerical answer: ________
Q2NAT2 marksPositions
In a 0-based max-heap of 31 distinct keys, how many different indices can hold the 3rd largest key?
Numerical answer: ________
Q3MCQ2 marksTernary
Which is a valid 0-based ternary max-heap?
- (A)
9 6 3 1 8 5 - (B)
9 3 6 8 5 1 - (C)
9 5 6 8 3 1 - (D)
9 1 6 8 3 5
Q4MCQ1 markk-th
The k-th largest of a max-heap (k small) can be found in:
- (A) O(log k)
- (B) O(n log n) only
- (C) O(k log n)
- (D) O(1)
Q5NAT2 marksTernary insert
Insert 7, 2, 10 (in that order) into the ternary max-heap 9 5 6 8 3 1. Which key is at index 2 at the end?
Numerical answer: ________
Q6MSQ1 markd-ary
For a d-ary heap with n keys (d ≥ 2), which are true?
- (A) Its height is about log_d n.
- (B) Deleting the root compares up to d children per level.
- (C) It need not be complete.
- (D) Insertion is cheaper than in a binary heap for large d.
Q7NAT1 markCount
How many distinct max-heaps on 5 distinct keys?
Numerical answer: ________
Q8MCQ2 marksOutput
What does this print?
int main(void)
{
int h[] = {100, 80, 90, 50, 60, 70, 40, 30, 55}, n = 9, i, min = h[n / 2];
for (i = n / 2 + 1; i < n; i++) /* only the leaves can hold the minimum */
if (h[i] < min) min = h[i];
printf("%d %d\n", min, n - n / 2);
return 0;
}
- (A)
55 5 - (B)
30 5 - (C)
20 5 - (D)
30 4
DAnswer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: 31 mark
Left 2, right 1: C(3, 2) × 1 × 1 = 3.
Re-read: Lesson 5.4 — Heap GATE drills: k-th largest, number of heaps, comparisons, d-ary heaps
Q2 Answer: 62 marks
Depth 1 or 2: indices 1 … 6: 6.
Re-read: Lesson 5.4 — Heap GATE drills: k-th largest, number of heaps, comparisons, d-ary heaps
Q3 Answer: (C)2 marks
true,false,false,false.
- (A) is wrong: 8 (index 4) is under 6 (index 1).
- (B) is wrong: 5 (index 4) is under 3 (index 1).
- (D) is wrong: 3 and 5 (indices 4, 5) are under 1.
Re-read: Lesson 5.4 — Heap GATE drills: k-th largest, number of heaps, comparisons, d-ary heaps
Q4 Answer: (C)1 mark
Delete the maximum k times.
- (A) is wrong: Too small.
- (B) is wrong: Much less is enough.
- (D) is wrong: Only k = 1.
Re-read: Lesson 5.4 — Heap GATE drills: k-th largest, number of heaps, comparisons, d-ary heaps
Q5 Answer: 92 marks
Result 10 7 9 8 3 1 5 2 6: 9.
Re-read: Lesson 5.4 — Heap GATE drills: k-th largest, number of heaps, comparisons, d-ary heaps
Q6 Answer: (A) (B) (D)1 mark
It is complete like a binary heap.
- (C) is wrong: The array layout needs completeness.
Re-read: Lesson 5.4 — Heap GATE drills: k-th largest, number of heaps, comparisons, d-ary heaps
Q7 Answer: 81 mark
C(4, 3) × 2 × 1 = 8.
Re-read: Lesson 5.4 — Heap GATE drills: k-th largest, number of heaps, comparisons, d-ary heaps
Q8 Answer: (B)2 marks
Leaves 60 70 40 30 55: minimum 30.
Output
30 5
- (A) is wrong: 55 is not the smallest leaf.
- (C) is wrong: 20 is no longer in the array.
- (D) is wrong: There are 5 leaves.
Re-read: Lesson 5.4 — Heap GATE drills: k-th largest, number of heaps, comparisons, d-ary heaps