Question 1Easy
Which array represents a valid min-heap?
- [2, 1, 3, 4, 5, 6, 7]
- [1, 2, 3, 4, 0, 5, 6]
- [1, 3, 2, 7, 4, 5, 6]
- [1, 4, 2, 3, 5, 6, 7]
Answer: C. [1, 3, 2, 7, 4, 5, 6]
Explanation
Each parent must be <= both children.
The others fail: 2 > 0 (index 1 and 4),
2 > 1 at the root, and 4 > 3 (index 1 and 3).
Step-by-step solution
- Check
parent <= children everywhere. - Option D:
1 <= 3, 2;
3 <= 7, 4;
2 <= 5, 6.
Valid.
- Others fail.
Why the other options are wrong
- A. The root 2 is larger than its child 1.
- B. 0 is smaller than its parent 2.
- D. 3 is smaller than its parent 4.
Question 2Easy
In a 0-indexed array representation of a binary heap, where are the children of the node at index i?
- 2i and 2i + 1
- 2i + 1 and 2i + 2
- i + 1 and i + 2
- i / 2 and i / 2 + 1
Answer: B. 2i + 1 and 2i + 2
Explanation
With 0-based indexing the parent of i is (i - 1) / 2 (integer division). With 1-based indexing the children are 2i and 2i + 1.
Step-by-step solution
- With 0-based indexing the root is at 0 and its children are at 1 and 2.
- In general the children of i are 2i + 1 and 2i + 2.
- The parent of i is (i - 1) // 2.
Why the other options are wrong
- A. 2i and 2i + 1 are for 1-based indexing.
- C. i + 1 and i + 2 are siblings or neighbours, not children.
- D. i / 2 is (roughly) the parent, not the children.
Tip: 0-based: children 2i+1, 2i+2; parent (i-1)//2.
Question 4Easy
What is the height of a binary heap with 1000 elements (a single node has height 0)?
- 10
- 500
- 9
- 8
Answer: C. 9
Explanation
floor(log2 1000) = 9, since 512 <= 1000 < 1024.
Step-by-step solution
- A heap is a complete binary tree, so height =
floor(log2 n). - 2^9 = 512 <= 1000 < 1024 = 2^10.
- Height = 9.
Why the other options are wrong
- A. 10 would need at least 1024 nodes.
- B. That is the number of leaves, roughly, not the height.
- D. A height-8 tree holds at most 511 nodes.
Tip: Heap height = floor(log2 n).
Question 5Medium
Is heap sort stable, and what is its extra space?
- Not stable; O(1) extra space
- Stable; O(1) extra space
- Stable; O(n) extra space
- Not stable; O(n) extra space
Answer: A. Not stable; O(1) extra space
Explanation
Long-distance swaps can reorder equal keys, but the sort works in place within the array.
Step-by-step solution
- Heap sort swaps distant elements, which can reorder equal keys.
- So it is not stable.
- It works inside the array: O(1) extra space.
Why the other options are wrong
- B. Heap sort is not stable.
- C. Neither stable nor using O(n) space.
- D. It needs only O(1) extra space.
Question 6Medium
In the max-heap [50, 30, 40, 10, 20, 35], the key 10 (index 3) is increased to 60. What is the array after restoring the heap property?
- [60, 50, 40, 10, 30, 35]
- [60, 50, 40, 30, 20, 35]
- [50, 30, 40, 60, 20, 35]
- [60, 30, 50, 40, 20, 35]
Answer: B. [60, 50, 40, 30, 20, 35]
Explanation
60 sifts up: it swaps with its parent 30 (index 1) and then with the root 50, giving [60, 50, 40, 30, 20, 35].
Step-by-step solution
- Set a[3] = 60: [50, 30, 40, 60, 20, 35].
- Parent of 3 is 1 (30): 60 > 30, swap -> [50, 60, 40, 30, 20, 35].
- Parent of 1 is 0 (50): 60 > 50, swap -> [60, 50, 40, 30, 20, 35].
Why the other options are wrong
- A. 10 no longer exists (it became 60), and 20 should stay at index 4; 30 moves down to index 3.
- C. The heap property is not restored; 60 sits below 30.
- D. 60 only travels along its own parent path (3 -> 1 -> 0), not via index 2.
Question 7Medium
What is the maximum number of elements in a binary heap of height h (a single node has height 0)?
- 2h + 1
- 2^(h+1) - 1
- 2^h - 1
- 2^h
Answer: B. 2^(h+1) - 1
Explanation
A complete tree of height h with all levels full has 1 + 2 + ... + 2^h nodes.
Step-by-step solution
- A full heap of height h has levels 0 to h.
- Level i has 2^i nodes.
- Total 2^(h+1) - 1.
Why the other options are wrong
- A. Linear growth is wrong for trees.
- C. That is for height h - 1.
- D. That counts only the bottom level.
Tip: Height h heap: between 2^h and 2^(h+1) - 1 nodes.
Question 8Medium
To track the 3 largest values of the stream [7, 2, 9, 4, 11, 3, 8, 10], a MIN-heap of size 3 is used (push each value, pop the minimum whenever the size exceeds 3). At the end, what is at the root of the heap?
- 11
- 9
- 4
- 10
Answer: B. 9
Explanation
The heap ends holding the 3 largest values [9, 10, 11], and its root (the minimum of them) is the 3rd largest, 9.
Step-by-step solution
- Keep a min-heap of the 3 largest seen so far.
- After 7, 2, 9: {2, 7, 9}; 4 replaces 2; 11 replaces 4; 3 is dropped; 8 replaces 7; 10 replaces 8.
- Final heap {9, 10, 11}; root = 9.
Why the other options are wrong
- A. 11 is the largest, at the bottom of a min-heap.
- C. 4 was evicted.
- D. 10 is not the minimum of {9, 10, 11}.
Tip: k largest -> min-heap of size k; k smallest -> max-heap.
Question 9Hard
Which heap supports decrease-key in O(1) amortised time, improving Dijkstra to O(E + V log V)?
- Fibonacci heap
- d-ary heap
- Binomial heap
- Binary heap
Answer: A. Fibonacci heap
Explanation
Fibonacci heaps defer consolidation, so decrease-key is amortised O(1) and extract-min is O(log n).
Step-by-step solution
- Dijkstra performs up to E decrease-key operations.
- Fibonacci heaps do decrease-key in O(1) amortised.
- Total O(E + V log V).
Why the other options are wrong
- B. d-ary heaps still have O(log n) decrease-key.
- C. Binomial heaps take O(log n) for decrease-key.
- D. Binary heaps take O(log n).
Question 10Hard
How many swaps does bottom-up heapify perform to turn [1, 2, 3, 4, 5, 6, 7] into a max-heap?
- 4
- 7
- 10
- 3
Answer: A. 4
Explanation
Index 2 (3) swaps with 7, index 1 (2) swaps with 5, and the root 1 sifts down two levels (with 7, then 6): 4 swaps. Inserting one by one would take 10.
Step-by-step solution
- i=2: 3 swaps with 7. i=1: 2 swaps with 5.
- i=0: 1 swaps with 7, then with 6.
- Total 4 swaps.
Why the other options are wrong
- B. Counts one per element.
- C. Too many; leaves never sift.
- D. Misses the second swap at the root.