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, where all levels are fully filled except possibly the last level, 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 represented 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.
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 while maintaining the heap property.
Click to reveal answer
What property must a binary tree satisfy to be a heap?
AComplete binary tree and heap property
BFull binary tree only
CBalanced binary tree only
DBinary search tree property
✗ Incorrect
A heap must be a complete binary tree and satisfy the heap property (max or min).
In a max heap, the root node is:
AThe smallest element
BThe largest element
CRandom element
DThe median element
✗ Incorrect
In a max heap, the root node is always the largest element.
How do you find the left child index of a node at index i in a heap array?
Ai/2
B2*i
C2*i + 1
Di - 1
✗ Incorrect
Left child index is calculated as 2*i + 1.
Which of these is NOT true about heaps?
AHeaps maintain sorted order of elements
BHeaps can be represented as arrays
CHeaps allow efficient priority access
DHeaps are complete binary trees
✗ Incorrect
Heaps do not maintain sorted order, only the heap property.
What is the parent index of a node at index i in a heap array?
Ai + 1
B2*i + 1
C2*i
D(i-1)//2
✗ Incorrect
Parent index is calculated as (i-1)//2.
Describe the two main properties that define a heap data structure.
Think about the tree shape and how parent and child values relate.
You got /4 concepts.
Explain how a heap is stored in an array and how to find parent and child nodes using indices.
Focus on the formulas for navigating the heap in an array.
You got /5 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. The tree is always a complete binary tree
Solution
Step 1: Understand max-heap property
A max-heap requires that each parent node is greater than or equal to its children nodes.
Step 2: Compare options with max-heap 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. All leaf nodes are at the same level and D are about tree shape, not heap order.
Final Answer:
Every parent node is greater than or equal to its children -> Option B
Quick Check:
Max-heap property = parent ≥ children [OK]
Hint: Max-heap means parent ≥ children always [OK]
Common Mistakes:
Confusing max-heap with min-heap property
Thinking heap requires all leaves at same level
Mixing heap order with tree completeness
2. Which of the following is the correct way to declare a min-heap using Go's container/heap package?
easy
A. var h []int = make([]int, 0) // slice for heap
B. var h heap.Interface = []int{} // direct slice assignment
C. var h = heap.NewMinHeap() // built-in min heap constructor
D. var h heap.Interface = &IntHeap{} // IntHeap implements heap.Interface
Solution
Step 1: Recall Go heap package usage
Go's container/heap requires a type implementing heap.Interface, like IntHeap struct.
Step 2: Analyze options for correct syntax
var h heap.Interface = &IntHeap{} // IntHeap implements heap.Interface correctly declares a heap.Interface variable with a pointer to IntHeap. var h []int = make([]int, 0) // slice for heap is just a slice, not a heap. var h = heap.NewMinHeap() // built-in min heap constructor is invalid; no NewMinHeap exists. var h heap.Interface = []int{} // direct slice assignment assigns a slice directly to heap.Interface, which is invalid.
Final Answer:
var h heap.Interface = &IntHeap{} // IntHeap implements heap.Interface -> Option D
Quick Check:
Heap needs heap.Interface implementation [OK]
Hint: Heap in Go needs heap.Interface implementation [OK]
Common Mistakes:
Using slice directly without implementing heap.Interface
Assuming built-in heap constructors exist
Assigning slice to heap.Interface without pointer
3. Consider the following Go code using a max-heap implemented with container/heap:
A. Less method uses > instead of <; change to h[i] < h[j]
B. Push method should use pointer receiver; change to (h IntHeap)
C. Pop method returns wrong element; should return old[0]
D. Heap must be initialized with make([]int, 0) instead of literal
Solution
Step 1: Identify heap type from Less method
The Less method uses h[i] > h[j], which creates a max-heap, not a min-heap.
Step 2: Fix Less method for min-heap
To make a min-heap, Less should return h[i] < h[j]. This fixes the ordering so smallest element is on top.
Final Answer:
Less method uses > instead of <; change to h[i] < h[j] -> Option A
Quick Check:
Min-heap requires Less = h[i] < h[j] [OK]
Hint: Min-heap Less returns h[i] < h[j] [OK]
Common Mistakes:
Confusing Less method direction for min vs max heap
Changing Push receiver incorrectly
Misunderstanding Pop return value
5. You have a list of integers: [7, 2, 5, 3, 9, 1]. You want to find the 3 largest elements efficiently using a min-heap of size 3. Which approach correctly uses heap properties to solve this?
hard
A. Sort the list and pick last 3 elements
B. Build a max-heap with all elements, then pop 3 times to get largest elements
C. Build a min-heap with first 3 elements, then for each remaining element, if greater than heap root, replace root and heapify
D. Build a min-heap with all elements and pop 3 times
Solution
Step 1: Understand problem and heap size constraint
We want top 3 largest elements efficiently, so keep a min-heap of size 3 to track current largest.
Step 2: Use min-heap to track largest elements
Build min-heap with first 3 elements. For each remaining element, if it is larger than heap root (smallest in heap), replace root and heapify. This keeps 3 largest elements in heap.
Step 3: Analyze options
Build a min-heap with first 3 elements, then for each remaining element, if greater than heap root, replace root and heapify matches this approach. Build a max-heap with all elements, then pop 3 times to get largest elements uses max-heap on all elements, which is less efficient. Sort the list and pick last 3 elements sorts entire list, which is more work. Build a min-heap with all elements and pop 3 times pops smallest elements, not largest.
Final Answer:
Build a min-heap with first 3 elements, then for each remaining element, if greater than heap root, replace root and heapify -> Option C
Quick Check:
Min-heap size 3 tracks top 3 largest [OK]
Hint: Use min-heap of size k to track k largest elements [OK]
Common Mistakes:
Using max-heap on all elements (less efficient)
Sorting entire list unnecessarily
Using min-heap on all elements and popping smallest
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.