A heap is a special tree-based data structure that satisfies the heap property: in a max heap, every parent node is greater than or equal to its children; in a min heap, every parent node is less than or equal to its children.
Click to reveal answer
beginner
What is the shape property of a heap?
The shape property means the heap is a complete binary tree, which means all levels are fully filled except possibly the last level, which is filled from left to right.
Click to reveal answer
beginner
Explain the heap property with an example.
In a max heap, if a parent node has value 10, its children must have values less than or equal to 10. For example, parent 10 with children 8 and 5 satisfies the heap property.
Click to reveal answer
intermediate
How is a heap usually stored in memory?
A heap is usually stored as an array where the parent and child relationships are determined by indices: for a node at index i, left child is at 2*i + 1, right child at 2*i + 2, and parent at (i-1)/2 (integer division).
Click to reveal answer
intermediate
Why is the heap structure useful for priority queues?
Because the heap property allows quick access to the highest (max heap) or lowest (min heap) priority element at the root, making insertion and removal operations efficient (logarithmic time).
Click to reveal answer
What type of tree structure does a heap follow?
ABinary search tree
BFull binary tree
CComplete binary tree
DAVL tree
✗ Incorrect
A heap is always a complete binary tree, meaning all levels are fully filled except possibly the last, which is filled from left to right.
In a max heap, what is true about the parent and child nodes?
AParent is greater than or equal to children
BParent is less than children
CParent is equal to children
DNo relation
✗ Incorrect
The max heap property requires each parent node to be greater than or equal to its children.
How do you find the left child index of a node at index i in a heap array?
Ai / 2
B2 * i + 1
C2 * i
Di - 1
✗ Incorrect
In array representation of a heap, left child index = 2 * i + 1.
Which of these is NOT a property of a heap?
ABinary search property
BShape property
CHeap property
DComplete binary tree
✗ Incorrect
Heaps do not follow the binary search tree property; they only follow heap and shape properties.
Why is a heap efficient for implementing a priority queue?
ABecause it uses hash tables internally
BBecause it sorts elements automatically
CBecause it stores elements in linked lists
DBecause it allows quick access to the highest or lowest priority element
✗ Incorrect
The heap structure allows quick access to the root element, which is the highest or lowest priority, making priority queue operations efficient.
Describe the two main properties that define a heap and why they are important.
Think about the tree structure and the order of elements.
You got /3 concepts.
Explain how a heap is stored in an array and how to find parent and child nodes using indices.
Use simple math formulas for index calculations.
You got /3 concepts.
Practice
(1/5)
1. What is a key property of a max-heap data structure?
easy
A. The tree is always a complete binary tree but not necessarily balanced
B. The value of each parent node is less than or equal to its children
C. All leaf nodes have the same value
D. The value of each parent node is greater than or equal to its children
Solution
Step 1: Understand max-heap property
A max-heap requires that every parent node's value is greater than or equal to its children.
Step 2: Compare options with max-heap definition
The value of each parent node is greater than or equal to its children matches the max-heap property exactly, while others describe min-heap or unrelated properties.
Final Answer:
The value of each parent node is greater than or equal to its children -> Option D
Quick Check:
Max-heap parent ≥ children = A [OK]
Hint: Max-heap means parent node ≥ children nodes [OK]
Common Mistakes:
Confusing max-heap with min-heap property
Thinking leaf nodes must have same value
Ignoring the complete binary tree requirement
2. Which of the following is the correct way to declare a min-heap using Java's PriorityQueue?
easy
A. PriorityQueue<Integer> minHeap = new PriorityQueue<>(Collections.reverseOrder());
B. PriorityQueue<Integer> minHeap = new PriorityQueue<>(Comparator.reverseOrder());
C. PriorityQueue<Integer> minHeap = new PriorityQueue<>();
D. PriorityQueue<Integer> minHeap = new PriorityQueue<>(Collections.naturalOrder());
By default, PriorityQueue in Java is a min-heap, so no comparator is needed.
Step 2: Analyze options for min-heap declaration
PriorityQueue<Integer> minHeap = new PriorityQueue<>(); uses default constructor, which creates a min-heap. A and B use reverseOrder creating max-heap, C uses non-existent Collections.naturalOrder().
Final Answer:
PriorityQueue<Integer> minHeap = new PriorityQueue<>(); -> Option C
Quick Check:
Default PriorityQueue = min-heap = D [OK]
Hint: Default PriorityQueue in Java is min-heap [OK]
Common Mistakes:
Using reverseOrder() creates max-heap, not min-heap
Confusing naturalOrder() with reverseOrder()
Adding unnecessary comparator for default min-heap
3. What will be the output of the following Java code snippet?
import java.util.PriorityQueue;
public class TestHeap {
public static void main(String[] args) {
PriorityQueue<Integer> heap = new PriorityQueue<>();
heap.add(30);
heap.add(10);
heap.add(20);
System.out.print(heap.poll() + ", " + heap.poll() + ", " + heap.poll());
}
}
medium
A. 20, 10, 30
B. 10, 20, 30
C. 30, 20, 10
D. 10, 30, 20
Solution
Step 1: Understand PriorityQueue behavior
Java's PriorityQueue is a min-heap by default, so poll() returns the smallest element.
Step 2: Trace poll() calls
Elements added: 30, 10, 20. First poll() returns 10, second 20, third 30.
Final Answer:
10, 20, 30 -> Option B
Quick Check:
poll() returns min element each time = 10,20,30 [OK]
Hint: poll() returns smallest element in min-heap [OK]
Common Mistakes:
Assuming poll() returns elements in insertion order
Confusing min-heap with max-heap behavior
Not realizing poll() removes elements
4. Identify the error in the following code that attempts to create a max-heap using Java's PriorityQueue:
import java.util.PriorityQueue;
import java.util.Collections;
public class MaxHeapExample {
public static void main(String[] args) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
maxHeap.add(5);
maxHeap.add(15);
maxHeap.add(10);
System.out.println(maxHeap.poll());
}
}
medium
A. PriorityQueue is not initialized with a comparator for max-heap
B. PriorityQueue cannot store Integer objects
C. poll() method does not exist for PriorityQueue
D. add() method cannot be used to insert elements
Solution
Step 1: Check PriorityQueue initialization
The code initializes PriorityQueue without a comparator, so it defaults to min-heap.
Step 2: Identify max-heap creation mistake
To create max-heap, PriorityQueue must be initialized with Collections.reverseOrder() comparator.
Final Answer:
PriorityQueue is not initialized with a comparator for max-heap -> Option A
Quick Check:
Missing comparator = B [OK]
Hint: Max-heap needs reverseOrder() comparator in PriorityQueue [OK]
Common Mistakes:
Assuming default PriorityQueue is max-heap
Thinking poll() method is invalid
Believing add() cannot insert elements
5. Given a max-heap represented as an array: [50, 30, 40, 10, 20], which of the following arrays represents the heap after inserting the element 35 and re-heapifying?
hard
A. [50, 30, 40, 10, 20, 35]
B. [50, 35, 40, 10, 20, 30]
C. [50, 40, 30, 10, 20, 35]
D. [50, 40, 35, 10, 20, 30]
Solution
Step 1: Insert 35 at the end of the array
Array becomes [50, 30, 40, 10, 20, 35].
Step 2: Re-heapify by comparing 35 with its parent
Parent of index 5 is index 2 (40). Since 35 <= 40, no swap needed. Heap property is maintained.
Step 3: Check if bubbling up is needed
Since 35 < 40, no bubble up occurs, so the array remains [50, 30, 40, 10, 20, 35]. However, this matches [50, 30, 40, 10, 20, 35], but [50, 30, 40, 10, 20, 35] is not the correct max-heap after insertion because 35 should bubble up if possible.
Step 4: Correct bubbling up
Actually, 35 is less than 40, so no swap with parent. The heap remains [50, 30, 40, 10, 20, 35].
Final Answer:
[50, 30, 40, 10, 20, 35] -> Option A
Quick Check:
Insert 35 at end, parent(5)=40 >= 35, no bubble up = D [OK]
Hint: Insert at end, then bubble up to fix max-heap [OK]
Common Mistakes:
Not bubbling up after insertion
Swapping with wrong parent index
Assuming insertion at root
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.