LearnFrenzy
Sign in Take a free test

DSA & Algorithms · Topic 9 of 13

Heaps and Priority Queues: questions with answers

Heap property and array layout, sift-up and sift-down, building a heap in O(n), heap sort, priority queues in Python, Java and C++, top-k, running median and merging.

Questions
45
Timed sets
3
Answers run or computed
26
Your accuracy
–

What this topic covers

16 subtopics
  • Heap operations7
  • Library heaps7
  • Array layout5
  • Heap basics4
  • Top-k4
  • Building a heap3
  • Heap variants3
  • Applications2
  • Greedy with heaps2
  • Heap sort2
  • Choosing a structure1
  • Generating sequences1
Show all 16 subtopics
  • Merging1
  • Scheduling1
  • Simulation1
  • Two heaps1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

A binary heap is a complete binary tree stored in an array: the children of index i are 2i + 1 and 2i + 2, its parent is (i − 1) // 2. In a max-heap every parent is ≥ its children, so the maximum is at index 0. Insert by appending and sifting up; remove the top by moving the last element to the root and sifting down – both O(log n). Heaps power priority queues, top-k problems, Dijkstra and heap sort. Each heap below is drawn as a tree with its array underneath.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Heap basics Beginner Practice Answer computed

Which array is a valid max-heap?

  1. 90
  2. 15
  3. 10
  4. 7
  5. 16
  6. 2
  1. 90
  2. 10
  3. 15
  4. 12
  5. 7
  6. 20
  1. 90
  2. 15
  3. 10
  4. 7
  5. 12
  6. 2
  1. 10
  2. 15
  3. 90
  4. 7
  5. 12
  6. 2
Show answer and explanation
Correct answer C

Explanation

Check each parent against its children: 90 ≥ 15, 10; 15 ≥ 7, 12; 10 ≥ 2. In B, 16 is a child of 15. In C the root is not the largest. In D, 20 is a child of 15.

Why the other options are wrong

  • A16 is greater than its parent 15.
  • B20 is greater than its parent 15.
  • DThe root must be the maximum.
Question 2 Building a heap Intermediate Practice Answer computed

Bottom-up heapify turns this array into a min-heap (sifting down from the last parent to the root). What is the result?

Array
a
  1. 03
  2. 19
  3. 22
  4. 31
  5. 44
  6. 55
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 9
  1. 2
  2. 1
  3. 3
  4. 9
  5. 4
  6. 5
  1. 1
  2. 3
  3. 2
  4. 4
  5. 9
  6. 5
  1. 1
  2. 3
  3. 2
  4. 9
  5. 4
  6. 5
Show answer and explanation
Correct answer D

Explanation

Index 2 (2) is already smaller than 5. Index 1 (9) swaps with its smaller child 1. Index 0 (3) swaps with its smaller child 1 → [1, 3, 2, 9, 4, 5]. Heapify does not sort.

Why the other options are wrong

  • AThat is sorted, not just a heap.
  • BThe root must be the minimum.
  • C9 stays at index 3 after its swap.
Question 3 Building a heap Advanced Practice Answer computed

Bottom-up build of a max-heap from the sorted array [1, 2, 3, 4, 5, 6, 7]. How many swaps are made in total?

Before building
4251637

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 4

Explanation

Index 2 (3) swaps with 7; index 1 (2) swaps with 5; index 0 (1) swaps with 7, then with 6 below it. 1 + 1 + 2 = 4 swaps, giving [7, 5, 6, 4, 2, 1, 3].

42 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

DSA & Algorithms

All 13 topics

Topics, levels and full mocks
  1. 01 Time and Space Complexity45 questions · 3 levels
  2. 02 Arrays and Strings45 questions · 3 levels
  3. 03 Linked Lists45 questions · 3 levels
  4. 04 Stacks and Queues45 questions · 3 levels
  5. 05 Hashing45 questions · 3 levels
  6. 06 Recursion and Backtracking45 questions · 3 levels
  7. 07 Sorting and Searching45 questions · 3 levels
  8. 08 Trees and BST45 questions · 3 levels
  9. 09 Heaps and Priority Queues45 questions · 3 levels
  10. 10 Graphs45 questions · 3 levels
  11. 11 Dynamic Programming45 questions · 3 levels
  12. 12 Greedy and Bit Manipulation45 questions · 3 levels
  13. 13 GATE-Style DSA & Algorithms Questions (MSQ & NAT)45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links