Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a min-heap?
A min-heap is a special tree-based data structure where the value of each parent node is less than or equal to the values of its children. This means the smallest value is always at the root.
Click to reveal answer
beginner
What is a max-heap?
A max-heap is a tree-based data structure where the value of each parent node is greater than or equal to the values of its children. This means the largest value is always at the root.
Click to reveal answer
beginner
What property must every node satisfy in a min-heap?
Every node's value must be less than or equal to the values of its children nodes.
Click to reveal answer
intermediate
How does a max-heap maintain its structure after inserting a new element?
After insertion, the new element is placed at the bottom and then 'bubbled up' by swapping with its parent until the max-heap property is restored (parent is larger or equal).
Click to reveal answer
intermediate
Why are heaps useful for priority queues?
Heaps allow quick access to the highest or lowest priority element (root), and insertion or removal operations happen efficiently, making them ideal for priority queues.
Click to reveal answer
In a min-heap, where is the smallest element located?
AAt the root node
BAt the leaf nodes
CIn the middle level
DAt any random node
✗ Incorrect
The min-heap property ensures the smallest element is always at the root.
Which of the following is true about a max-heap?
AAll nodes have equal values
BParent nodes are smaller than children
CParent nodes are larger than or equal to children
DChildren nodes are always larger than the root
✗ Incorrect
In a max-heap, each parent node is greater than or equal to its children.
What happens when you insert a new element into a min-heap?
AIt is added at the bottom and moved up if smaller than parent
BIt is added randomly
CIt replaces the largest element
DIt is placed at the root immediately
✗ Incorrect
New elements are added at the bottom and moved up to maintain the min-heap property.
Which data structure is best for quickly finding the largest element?
AQueue
BMin-heap
CLinked list
DMax-heap
✗ Incorrect
A max-heap keeps the largest element at the root for quick access.
Why do heaps make good priority queues?
ABecause they store elements in sorted order
BBecause they allow fast access to highest or lowest priority
CBecause they use less memory
DBecause they are easy to implement
✗ Incorrect
Heaps allow quick access to the highest or lowest priority element, which is essential for priority queues.
Explain the main difference between a min-heap and a max-heap.
Think about which element is at the top in each heap.
You got /3 concepts.
Describe how a heap maintains its property after inserting a new element.
Consider the 'bubble up' or 'heapify' process.
You got /4 concepts.
Practice
(1/5)
1. Which of the following best describes a min-heap property?
easy
A. The tree is not necessarily complete.
B. The parent node is always larger than or equal to its children.
C. The root node is always the largest value.
D. The parent node is always smaller than or equal to its children.
Solution
Step 1: Understand min-heap property
A min-heap requires that every parent node is smaller than or equal to its children.
Step 2: Compare options with definition
The parent node is always smaller than or equal to its children. matches this definition exactly, while others describe max-heap or incorrect properties.
Final Answer:
The parent node is always smaller than or equal to its children. -> Option D
Quick Check:
Min-heap = parent ≤ children [OK]
Hint: Min-heap means smallest value at the top [OK]
Common Mistakes:
Confusing min-heap with max-heap property
Thinking the tree can be incomplete
Assuming root is largest in min-heap
2. Which of the following is the correct way to describe a max-heap?
easy
A. A binary tree where each parent is smaller than its children.
B. A complete binary tree where each parent is greater than or equal to its children.
C. A tree where the root is always the smallest value.
D. A binary tree that is not necessarily complete.
Solution
Step 1: Recall max-heap definition
A max-heap is a complete binary tree where each parent node is greater than or equal to its children.
Step 2: Match options to definition
A complete binary tree where each parent is greater than or equal to its children. correctly states this. Options A and C describe min-heap or incorrect properties, and D is false because heaps must be complete.
Final Answer:
A complete binary tree where each parent is greater than or equal to its children. -> Option B
Quick Check:
Max-heap = parent ≥ children [OK]
Hint: Max-heap means largest value at the root [OK]
Common Mistakes:
Mixing min-heap and max-heap definitions
Ignoring the completeness of the tree
Thinking root is smallest in max-heap
3. Given the max-heap array representation [50, 30, 40, 10, 20], what is the root value after inserting 60 and reheapifying?
medium
A. 60
B. 30
C. 40
D. 50
Solution
Step 1: Insert 60 at the end of the heap
Array becomes [50, 30, 40, 10, 20, 60].
Step 2: Reheapify by comparing 60 with its parent
60 is greater than 40 (its parent), so swap. New array: [50, 30, 60, 10, 20, 40]. Then compare 60 with 50 (new parent), swap again. Final array: [60, 30, 50, 10, 20, 40].
Final Answer:
60 -> Option A
Quick Check:
New root after insert = 60 [OK]
Hint: New max inserted bubbles up to root [OK]
Common Mistakes:
Not swapping inserted value up correctly
Confusing min-heap and max-heap behavior
Forgetting to reheapify after insertion
4. Identify the error in this min-heap array: [5, 3, 8, 10, 7].
medium
A. The root 5 is larger than child 3, violating min-heap property.
B. The tree is not complete.
C. The array is sorted incorrectly.
D. No error; this is a valid min-heap.
Solution
Step 1: Check min-heap property for root and children
Root is 5, left child is 3. Since 5 > 3, this violates the min-heap rule that parent ≤ children.
Step 2: Verify completeness and sorting
The tree is complete and array order is not required to be sorted, so no error there.
Final Answer:
The root 5 is larger than child 3, violating min-heap property. -> Option A
Quick Check:
Parent ≤ children violated at root [OK]
Hint: Parent must be ≤ children in min-heap [OK]
Common Mistakes:
Assuming array must be sorted
Ignoring parent-child value checks
Confusing completeness with heap property
5. You have a min-heap represented as [4, 10, 15, 20, 30]. You want to replace the root with 25 and restore the min-heap property. What will be the new root after reheapifying?
hard
A. 15
B. 4
C. 10
D. 20
Solution
Step 1: Replace root with 25
Array becomes [25, 10, 15, 20, 30].
Step 2: Reheapify by pushing 25 down
Compare 25 with children 10 and 15. The smallest child is 10, so swap 25 and 10. New array: [10, 25, 15, 20, 30].
Step 3: Continue reheapify
Now 25 is at index 1 with children 20 and 30. 25 ≤ 20 and 30 is false, so swap 25 with 20. New array: [10, 20, 15, 25, 30].
Step 4: Check if heap property holds
25 is now a leaf node, so heap property restored.
Final Answer:
10 -> Option C
Quick Check:
New root after replace and reheapify = 10 [OK]
Hint: Replace root, push down smaller child until heap restored [OK]