Heaps Questions with Answers

44 practice questions on Heaps (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 44 free in the AptiNest app.

Sample questions

Question 1Easy

Which array represents a valid min-heap?

  1. [2, 1, 3, 4, 5, 6, 7]
  2. [1, 2, 3, 4, 0, 5, 6]
  3. [1, 3, 2, 7, 4, 5, 6]
  4. [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

  1. Check parent <= children everywhere.
  2. Option D: 1 <= 3, 2; 3 <= 7, 4; 2 <= 5, 6. Valid.
  3. 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?

  1. 2i and 2i + 1
  2. 2i + 1 and 2i + 2
  3. i + 1 and i + 2
  4. 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

  1. With 0-based indexing the root is at 0 and its children are at 1 and 2.
  2. In general the children of i are 2i + 1 and 2i + 2.
  3. 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 3Easy

In a max-heap, where can the smallest element be?

  1. Always at the last index
  2. In any leaf
  3. At the root
  4. At index 1

Answer: B. In any leaf

Explanation

The heap only orders parents above children, so the minimum can be any of the roughly n/2 leaves.

Step-by-step solution

  1. The smallest element has no smaller children.
  2. In a max-heap, it must be a leaf.
  3. Any leaf can hold it, not a fixed position.

Why the other options are wrong

  • A. The last index is a leaf but not necessarily the smallest.
  • C. The root is the maximum.
  • D. Index 1 is a child of the root.

Question 4Easy

What is the height of a binary heap with 1000 elements (a single node has height 0)?

  1. 10
  2. 500
  3. 9
  4. 8

Answer: C. 9

Explanation

floor(log2 1000) = 9, since 512 <= 1000 < 1024.

Step-by-step solution

  1. A heap is a complete binary tree, so height = floor(log2 n).
  2. 2^9 = 512 <= 1000 < 1024 = 2^10.
  3. 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?

  1. Not stable; O(1) extra space
  2. Stable; O(1) extra space
  3. Stable; O(n) extra space
  4. 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

  1. Heap sort swaps distant elements, which can reorder equal keys.
  2. So it is not stable.
  3. 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?

  1. [60, 50, 40, 10, 30, 35]
  2. [60, 50, 40, 30, 20, 35]
  3. [50, 30, 40, 60, 20, 35]
  4. [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

  1. Set a[3] = 60: [50, 30, 40, 60, 20, 35].
  2. Parent of 3 is 1 (30): 60 > 30, swap -> [50, 60, 40, 30, 20, 35].
  3. 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)?

  1. 2h + 1
  2. 2^(h+1) - 1
  3. 2^h - 1
  4. 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

  1. A full heap of height h has levels 0 to h.
  2. Level i has 2^i nodes.
  3. 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?

  1. 11
  2. 9
  3. 4
  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

  1. Keep a min-heap of the 3 largest seen so far.
  2. After 7, 2, 9: {2, 7, 9}; 4 replaces 2; 11 replaces 4; 3 is dropped; 8 replaces 7; 10 replaces 8.
  3. 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)?

  1. Fibonacci heap
  2. d-ary heap
  3. Binomial heap
  4. 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

  1. Dijkstra performs up to E decrease-key operations.
  2. Fibonacci heaps do decrease-key in O(1) amortised.
  3. 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?

  1. 4
  2. 7
  3. 10
  4. 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

  1. i=2: 3 swaps with 7. i=1: 2 swaps with 5.
  2. i=0: 1 swaps with 7, then with 6.
  3. 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.

Practise all 44 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Structures & Algorithms topics · All topics