Recall & Review
beginner
Why do we need a heap when we already have a sorted array?
A heap allows efficient insertion and deletion of elements while maintaining order, unlike a sorted array which is slow to update because it requires shifting elements.
Click to reveal answer
beginner
What operation is slow in a sorted array but fast in a heap?
Insertion and deletion are slow in a sorted array (O(n)) because elements must be shifted, but fast in a heap (O(log n)) due to its tree structure.
Click to reveal answer
intermediate
How does a heap maintain order differently from a sorted array?
A heap maintains a partial order where the parent node is always greater (max-heap) or smaller (min-heap) than its children, allowing quick access to the top element without full sorting.
Click to reveal answer
beginner
What is the time complexity of finding the minimum or maximum element in a sorted array vs a heap?
In a sorted array, finding min or max is O(1) because elements are ordered. In a heap, finding min or max is O(1) only for the root element (max in max-heap or min in min-heap). For the opposite extreme, it is O(n).
Click to reveal answer
intermediate
Why is a heap preferred for priority queue implementations over a sorted array?
Because heaps allow fast insertion and removal of the highest priority element (both O(log n)), while sorted arrays require O(n) for insertion and deletion.
Click to reveal answer
Which operation is inefficient in a sorted array compared to a heap?
✗ Incorrect
Insertion in a sorted array requires shifting elements, making it O(n), while in a heap it is O(log n).
What is the time complexity of deleting the top element in a heap?
✗ Incorrect
Deleting the top element in a heap requires reheapifying, which takes O(log n) time.
Why can't a sorted array efficiently support dynamic priority queue operations?
✗ Incorrect
Insertion and deletion in a sorted array require shifting elements, making these operations inefficient.
In a max-heap, where is the maximum element located?
✗ Incorrect
The max-heap property ensures the maximum element is always at the root.
Which data structure is best for fast insertion and removal of the highest priority element?
✗ Incorrect
Heap supports both insertion and removal in O(log n), making it ideal for priority queues.
Explain why heaps exist and what problems sorted arrays face in dynamic data scenarios.
Think about how data changes over time and how each structure handles updates.
You got /4 concepts.
Describe the main differences in time complexity between heaps and sorted arrays for insertion and deletion.
Focus on how elements move or shift in each structure.
You got /4 concepts.