A heap is a special tree-based 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: all levels are fully filled except possibly the last, which is filled from left to right without gaps.
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 can have values 9 and 7 but not 11. This ensures the parent is always larger or equal to children.
Click to reveal answer
intermediate
How is a heap usually stored in memory?
A heap is commonly stored as an array where the parent-child relationships are determined by indices: for a node at index i, left child is at 2*i + 1 and right child at 2*i + 2.
Click to reveal answer
intermediate
Why is a heap useful for priority queues?
Because a heap allows quick access to the highest (or lowest) priority element at the root, and insertion or removal operations can be done efficiently while maintaining heap properties.
Click to reveal answer
Which property ensures a heap is a complete binary tree?
ABalance property
BHeap property
CShape property
DOrdering property
✗ Incorrect
The shape property ensures the heap is a complete binary tree, meaning all levels are fully filled except possibly the last.
In a max heap, what is true about the parent node compared to its children?
AParent is equal or larger than children
BParent is smaller than children
CParent is always the smallest
DParent has no relation to children
✗ Incorrect
In a max heap, the parent node is always greater than or equal to its children.
How do you find the left child index of a node at index i in a heap stored as an array?
A2i + 1
Bi + 1
C2i
Di - 1
✗ Incorrect
The left child of node at index i is at index 2i + 1 in array representation.
What kind of tree structure is a heap?
ABinary search tree
BComplete binary tree
CFull binary tree
DAVL tree
✗ Incorrect
A heap is a complete binary tree, meaning all levels are filled except possibly the last, which is filled from left to right.
Why is a heap efficient for priority queue operations?
ABecause it stores elements in sorted order
BBecause it is a balanced binary search tree
CBecause it uses linked lists
DBecause it allows quick access to highest or lowest priority element
✗ Incorrect
A heap allows quick access to the highest or lowest priority element at the root, making priority queue operations efficient.
Describe the two main properties that define a heap and explain why each is important.
Think about the tree structure and the order of values.
You got /4 concepts.
Explain how a heap is stored in an array and how to find the parent and children indices for any element.
Use formulas involving the index i.
You got /4 concepts.
Practice
(1/5)
1. What is the main property of a max-heap data structure?
easy
A. Every parent node is less than or equal to its children
B. Every parent node is greater than or equal to its children
C. All leaf nodes are at the same level
D. It is a complete binary tree with sorted elements
Solution
Step 1: Understand max-heap property
A max-heap requires that each parent node's value is greater than or equal to its children's values.
Step 2: Compare options with definition
Every 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:
Every parent node is greater than or equal to its children -> Option B
Quick Check:
Max-heap property = Every parent ≥ children [OK]
Hint: Max-heap means parent ≥ children always [OK]
Common Mistakes:
Confusing max-heap with min-heap property
Thinking heap means sorted tree
Assuming all leaf nodes must be at same level
2. Which of the following is the correct way to declare a max-heap using C++ STL?
easy
A. std::priority_queue<int> maxHeap;
B. std::priority_queue maxHeap;
C. std::priority_queue maxHeap;
D. std::priority_queue maxHeap;
Solution
Step 1: Recall default priority_queue behavior
By default, std::priority_queue in C++ is a max-heap using std::less comparator.
Step 2: Check each declaration
std::priority_queue<int> maxHeap; uses default max-heap. std::priority_queue maxHeap; uses std::greater, which creates min-heap. std::priority_queue maxHeap; uses std::list which is invalid container for priority_queue. std::priority_queue maxHeap; uses std::list which is invalid container for priority_queue.
Final Answer:
std::priority_queue<int> maxHeap; -> Option A
Quick Check:
Default priority_queue = max-heap [OK]
Hint: Default priority_queue is max-heap in C++ STL [OK]
Common Mistakes:
Using std::greater for max-heap (it's for min-heap)
Using invalid container like std::list
Confusing comparator template parameters
3. What will be the output of the following C++ code snippet?
std::priority_queue<int> pq;
pq.push(10);
pq.push(5);
pq.push(20);
pq.push(1);
std::vector<int> result;
while (!pq.empty()) {
result.push_back(pq.top());
pq.pop();
}
for (int val : result) std::cout << val << " ";
medium
A. 10 20 5 1
B. 1 5 10 20
C. 20 10 5 1
D. 5 10 1 20
Solution
Step 1: Understand priority_queue behavior
std::priority_queue by default is a max-heap, so top() returns the largest element.
Step 2: Trace push and pop operations
Elements pushed: 10, 5, 20, 1. The popping order will be from largest to smallest: 20, 10, 5, 1.
Final Answer:
20 10 5 1 -> Option C
Quick Check:
Max-heap pop order = 20 10 5 1 [OK]
Hint: priority_queue pops largest first by default [OK]
Common Mistakes:
Assuming priority_queue pops smallest first
Confusing push order with pop order
Ignoring default max-heap behavior
4. Identify the error in the following code snippet that tries to create a min-heap using C++ STL:
A. priority_queue by default is a max-heap, so this code does not create a min-heap
B. Missing include directive for <queue>
C. Cannot push elements into priority_queue
D. The pop() function is used incorrectly
Solution
Step 1: Check default behavior of priority_queue
By default, std::priority_queue is a max-heap, so it returns the largest element first.
Step 2: Analyze code intent vs behavior
The code tries to create a min-heap but uses default priority_queue without comparator, so it behaves as max-heap.
Final Answer:
priority_queue by default is a max-heap, so this code does not create a min-heap -> Option A
Quick Check:
Default priority_queue = max-heap, not min-heap [OK]
Hint: Default priority_queue is max-heap, need comparator for min-heap [OK]
Common Mistakes:
Assuming default priority_queue is min-heap
Thinking pop() usage is wrong
Ignoring need for comparator to create min-heap
5. You want to merge two max-heaps represented as arrays: heap1 = [40, 30, 20] and heap2 = [35, 25, 15]. Which approach correctly merges them into a valid max-heap?
hard
A. Sort both arrays and merge them in ascending order
B. Insert all elements of heap2 into heap1 one by one without rebuilding
C. Replace the root of heap1 with the root of heap2 and heapify once
D. Concatenate both arrays and then build a max-heap from the combined array
Solution
Step 1: Understand heap merge requirements
To merge two heaps, simply concatenating is not enough; we must restore heap property by building a max-heap from combined elements.
Step 2: Evaluate options
Concatenate both arrays and then build a max-heap from the combined array correctly concatenates and rebuilds heap. Insert all elements of heap2 into heap1 one by one without rebuilding inserts elements one by one but is less efficient. Sort both arrays and merge them in ascending order sorts ascending which breaks max-heap property. Replace the root of heap1 with the root of heap2 and heapify once replaces root incorrectly and does not fix heap.
Final Answer:
Concatenate both arrays and then build a max-heap from the combined array -> Option D
Quick Check:
Merge heaps by combining and heapifying [OK]
Hint: Merge heaps by combining arrays then heapify [OK]
Common Mistakes:
Sorting arrays instead of heapifying
Replacing root without fixing heap
Inserting elements one by one inefficiently
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.