Why do we need a heap when we already have a sorted array?
A heap allows faster insertion and deletion of the smallest or largest element compared to a sorted array, which requires shifting many elements.
Click to reveal answer
beginner
What is the main inefficiency of a sorted array when inserting a new element?
Inserting a new element in a sorted array requires shifting elements to keep order, which takes O(n) time.
Click to reveal answer
intermediate
How does a heap improve deletion of the smallest or largest element compared to a sorted array?
A heap can remove the smallest or largest element in O(log n) time by reorganizing the tree, while a sorted array takes O(n) due to shifting elements.
Click to reveal answer
intermediate
What operation is very fast in a sorted array but slower in a heap?
Accessing the smallest or largest element is O(1) in a sorted array and O(1) in a heap as well; however, sorted array has slower insertion and deletion.
Click to reveal answer
beginner
Explain in simple terms why heaps are useful for priority queues.
Heaps let us quickly add new items and remove the highest priority item without sorting everything again, making them perfect for priority queues.
Click to reveal answer
What is the time complexity of inserting an element into a sorted array?
AO(n)
BO(log n)
CO(1)
DO(n log n)
✗ Incorrect
Inserting into a sorted array requires shifting elements to keep order, which takes O(n) time.
Which data structure allows faster removal of the smallest element?
ASorted array
BUnsorted array
CHeap
DLinked list
✗ Incorrect
Heap removes the smallest element in O(log n) time, faster than a sorted array's O(n) due to shifting.
Why is a heap preferred over a sorted array for priority queues?
ABecause heaps keep elements fully sorted
BBecause heaps allow fast insertion and removal of priority elements
CBecause heaps use less memory
DBecause heaps are easier to implement
✗ Incorrect
Heaps allow fast insertion and removal of highest priority elements without full sorting.
What is the time complexity of removing the smallest element from a heap?
AO(log n)
BO(n)
CO(1)
DO(n log n)
✗ Incorrect
Removing the smallest element from a heap takes O(log n) time due to reheapification.
Which operation is fastest in a sorted array compared to a heap?
AReordering elements
BDeletion
CInsertion
DAccessing smallest element
✗ Incorrect
Accessing the smallest element is O(1) in a sorted array because it is at a known position.
Explain why heaps exist and what problems they solve that sorted arrays cannot handle efficiently.
Think about how adding or removing elements affects order and speed.
You got /4 concepts.
Describe the main differences in performance between heaps and sorted arrays for insertion and deletion operations.
Focus on how many elements need to move or be reorganized.
You got /4 concepts.
Practice
(1/5)
1. Why do we use a heap instead of just a sorted array when we want to repeatedly access the smallest element?
easy
A. Because heaps allow faster insertion and deletion of the smallest element than sorted arrays.
B. Because heaps store elements in sorted order all the time.
C. Because sorted arrays use less memory than heaps.
D. Because heaps are easier to implement than sorted arrays.
Solution
Step 1: Understand operations on sorted arrays
Inserting or deleting elements in a sorted array requires shifting elements, which takes linear time (O(n)).
Step 2: Compare with heap operations
Heaps allow insertion and deletion of the smallest element in logarithmic time (O(log n)) by maintaining a special tree structure.
Final Answer:
Because heaps allow faster insertion and deletion of the smallest element than sorted arrays. -> Option A
Quick Check:
Heap operations faster than sorted array for insert/delete [OK]
Hint: Heaps speed up insert/delete of min element vs sorted arrays [OK]
Common Mistakes:
Thinking heaps keep all elements fully sorted
Assuming sorted arrays have faster insertions
Confusing memory usage with operation speed
2. Which of the following is the correct way to insert an element into a min-heap represented as an array in JavaScript?
easy
A. Replace the root with the new element and heapify down.
B. Insert element at the start and sort the array.
C. Add element at the end and do nothing else.
D. Add element at the end, then bubble it up to maintain heap property.
Solution
Step 1: Recall min-heap insertion process
Insert the new element at the end of the array to keep the complete tree shape.
Step 2: Restore heap property by bubbling up
Compare the new element with its parent and swap if smaller, repeat until heap property is restored.
Final Answer:
Add element at the end, then bubble it up to maintain heap property. -> Option D
Quick Check:
Insert by adding end + bubble up [OK]
Hint: Insert at end, then bubble up to fix heap [OK]
Common Mistakes:
Sorting the whole array after insertion
Not bubbling up after insertion
Replacing root directly without insertion
3. Consider this JavaScript code using a min-heap array representation:
const heap = [2, 5, 3, 10, 7];
// Insert 1 into heap
heap.push(1);
// Bubble up process
let i = heap.length - 1;
while (i > 0) {
let parent = Math.floor((i - 1) / 2);
if (heap[i] < heap[parent]) {
[heap[i], heap[parent]] = [heap[parent], heap[i]];
i = parent;
} else {
break;
}
}
console.log(heap);
What is the output after insertion and bubbling up?
Hint: Bubble up swaps until parent smaller or root reached [OK]
Common Mistakes:
Swapping with wrong parent index
Stopping bubble up too early
Incorrect array after swaps
4. The following code tries to remove the smallest element from a min-heap represented as an array. What is the bug?
function removeMin(heap) {
if (heap.length === 0) return null;
const min = heap[0];
heap[0] = heap.pop();
let i = 0;
while (true) {
let left = 2 * i + 1;
let right = 2 * i + 2;
let smallest = i;
if (left < heap.length && heap[left] < heap[smallest]) smallest = left;
if (right < heap.length && heap[right] < heap[smallest]) smallest = right;
if (smallest === i) break;
[heap[i], heap[smallest]] = [heap[smallest], heap[i]];
i = smallest;
}
return min;
}
medium
A. It does not handle the case when heap has only one element.
B. It does not handle empty heap correctly.
C. It does not check if heap.pop() returns undefined.
D. It does not update the index i correctly after swapping.
Solution
Step 1: Analyze removal when heap has one element
If heap has one element, heap.pop() removes it, then heap[0] = undefined sets root to undefined, causing invalid heap state.
Step 2: Check code for single element case
Code does not handle this special case; it should return the popped element directly without reassigning heap[0].
Final Answer:
It does not handle the case when heap has only one element. -> Option A
Quick Check:
Single element removal needs special handling [OK]
Hint: Check single-element heap removal edge case [OK]
Common Mistakes:
Ignoring single-element heap case
Assuming pop always returns a value
Not updating index after swap (actually correct here)
5. You want to maintain a collection of numbers where you frequently insert new numbers and also frequently remove the smallest number. Why is a heap better than a sorted array for this use case, and what is the time complexity difference?
hard
A. Heap allows insert and remove-min in O(log n), while sorted array requires O(n) for insert and O(1) for remove-min.
B. Heap allows insert and remove-min in O(log n), while sorted array requires O(n) for both insert and remove-min.
C. Heap allows insert and remove-min in O(1), while sorted array requires O(n) for insert and remove-min.
D. Heap allows insert and remove-min in O(n), while sorted array requires O(log n) for insert and remove-min.
Solution
Step 1: Analyze heap operation times
Insertion and removal of smallest element in a heap both take O(log n) time due to bubbling up/down.
Step 2: Analyze sorted array operation times
Insertion requires shifting elements to keep sorted order, O(n). Removing smallest (first element) requires shifting all remaining elements left, O(n).
Final Answer:
Heap allows insert and remove-min in O(log n), while sorted array requires O(n) for both insert and remove-min. -> Option B