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: 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 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 represented in memory?
A heap is usually 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, right child at 2 * i + 2, and parent at (i - 1) / 2.
Click to reveal answer
intermediate
Why is the heap structure useful for priority queues?
Because heaps allow quick access to the highest (max-heap) or lowest (min-heap) priority element at the root, and insertion or removal operations can be done efficiently in O(log n) time.
Click to reveal answer
What property must a heap always satisfy?
ABinary search tree property
BNodes are sorted in ascending order
CAll nodes have two children
DHeap property where parent nodes are ordered with respect to children
✗ Incorrect
A heap must satisfy the heap property where each parent node is ordered with respect to its children (max or min).
How is a heap typically stored in memory?
ALinked list
BArray
CHash table
DGraph
✗ Incorrect
Heaps are typically stored as arrays to efficiently represent the complete binary tree structure.
In a max-heap, what is true about the root node?
AIt has the largest value
BIt has no children
CIt has the smallest value
DIt is always a leaf node
✗ Incorrect
In a max-heap, the root node contains the largest value in the heap.
What does the shape property of a heap ensure?
AThe heap is a complete binary tree
BThe heap is balanced like an AVL tree
CThe heap is sorted
DThe heap has only one child per node
✗ Incorrect
The shape property ensures the heap is a complete binary tree, meaning all levels are fully filled except possibly the last.
What is the time complexity to insert an element into a heap?
AO(1)
BO(n)
CO(log n)
DO(n log n)
✗ Incorrect
Insertion in a heap takes O(log n) time due to the need to maintain the heap property by moving the new element up.
Describe the two main properties that define a heap and why they are important.
Think about how the tree looks and how parent and child values relate.
You got /3 concepts.
Explain how a heap is represented using an array and how to find parent and child nodes using indices.
Remember the formulas: left child = 2i + 1, right child = 2i + 2, parent = (i - 1) / 2.
You got /3 concepts.
Practice
(1/5)
1. What is a key property of a max-heap data structure?
easy
A. Every parent node is greater than or equal to its children
B. Every parent node is less than or equal to its children
C. All leaf nodes have the same value
D. The tree is always a complete binary tree with sorted leaves
Solution
Step 1: Understand max-heap property
A max-heap requires that each parent node's value is greater than or equal to the values of its children.
Step 2: Compare options with definition
Every parent node is greater than or equal to its children matches the max-heap property exactly. Every parent node is less than or equal to its children describes a min-heap. Options A and C are incorrect properties.
Final Answer:
Every parent node is greater than or equal to its children -> Option A
Quick Check:
Max-heap = parent ≥ children [OK]
Hint: Max-heap means parent ≥ children values [OK]
Common Mistakes:
Confusing max-heap with min-heap property
Thinking leaves must be sorted
Assuming all nodes have equal values
2. Which of the following is the correct way to declare a min-heap using C#'s PriorityQueue?
easy
A. var minHeap = new PriorityQueue();
B. var minHeap = new PriorityQueue(Comparer<int>.Default);
C. var minHeap = new PriorityQueue(Comparer<int>.Create((x, y) => y - x));
D. var minHeap = new PriorityQueue(Comparer<int>.Create((x, y) => x + y));
Solution
Step 1: Recall default behavior of PriorityQueue
In C#, PriorityQueue by default acts as a min-heap using the natural order of TPriority.
Step 2: Analyze options
var minHeap = new PriorityQueue(); uses default constructor, creating a min-heap. var minHeap = new PriorityQueue(Comparer<int>.Default); incorrectly tries to pass a comparer to constructor which does not exist. var minHeap = new PriorityQueue(Comparer<int>.Create((x, y) => y - x)); reverses order for max-heap. var minHeap = new PriorityQueue(Comparer<int>.Create((x, y) => x + y)); uses invalid comparer logic.
Final Answer:
var minHeap = new PriorityQueue<int, int>(); -> Option A
Quick Check:
Default PriorityQueue is min-heap [OK]
Hint: Default PriorityQueue is min-heap without comparer [OK]
Common Mistakes:
Trying to pass comparer to constructor (not supported)
Using wrong comparer logic for min-heap
Confusing min-heap and max-heap syntax
3. Consider the following C# code using a max-heap implemented as a binary heap array:
int[] heap = { 50, 30, 40, 10, 20 };
// Insert 60 and then heapify up
Insert(heap, 60);
What will be the heap array after insertion?
medium
A. [60, 50, 40, 10, 20, 60]
B. [60, 30, 50, 10, 20, 40]
C. [60, 50, 40, 30, 20, 10]
D. [50, 30, 40, 10, 20, 60]
Solution
Step 1: Insert 60 at the end of heap array
Initial heap: [50, 30, 40, 10, 20]. Insert 60 at index 5: [50, 30, 40, 10, 20, 60]
Step 2: Heapify up to maintain max-heap
Compare 60 with parent at index 2 (40). 60 > 40, swap. New array: [50, 30, 60, 10, 20, 40]. Compare 60 with parent at index 0 (50). 60 > 50, swap. Final array: [60, 30, 50, 10, 20, 40]
Final Answer:
[60, 30, 50, 10, 20, 40] -> Option B
Quick Check:
Insert + heapify up = [60,30,50,10,20,40] [OK]
Hint: Insert at end, swap up while parent smaller [OK]
Common Mistakes:
Not swapping with parent correctly
Placing new element at wrong index
Confusing heapify up with heapify down
4. The following C# code attempts to remove the root from a max-heap stored in an array but has a bug:
What is the main issue causing incorrect heap behavior?
medium
A. HeapifyDown is called with wrong starting index
B. The last element is not removed before assignment
C. The size variable is decremented locally but not updated outside
D. The root is not replaced with the maximum child
Solution
Step 1: Analyze size decrement scope
The size variable is decremented inside the method but since it's passed by value, the caller's size remains unchanged.
Step 2: Effect on heap operations
Because size outside is not updated, heap operations may access invalid elements or not reflect the reduced heap size, causing incorrect behavior.
Final Answer:
Size variable is decremented locally but not updated outside -> Option C
Quick Check:
Size must update outside method [OK]
Hint: Pass size by ref or return new size [OK]
Common Mistakes:
Assuming size changes persist without ref
Thinking heapify down index is wrong
Ignoring size update importance
5. You have a min-heap represented as an array: [5, 10, 15, 20, 25, 30]. You want to replace the root with 22 and restore the min-heap property. Which sequence of array states correctly shows the heap after replacing and heapifying down?
Compare 22 with children 10 and 15. Smallest child is 10. Swap 22 and 10: [10, 22, 15, 20, 25, 30]. Now 22 at index 1 has children at indices 3 (20) and 4 (25). Smallest is 20 < 22, swap 22 and 20: [10, 20, 15, 22, 25, 30]. 22 now at index 3 has no children, stop.