Recall & Review
beginner
Why do we need a heap when we already have a sorted array?
A heap allows fast insertion and deletion of the smallest or largest element, while a sorted array requires shifting many elements, making these operations slow.
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 time proportional to the array size (O(n)).
Click to reveal answer
beginner
How does a heap improve the efficiency of finding the smallest or largest element compared to an unsorted array?
A heap keeps the smallest or largest element at the root, so it can be found in constant time (O(1)), unlike an unsorted array which requires scanning all elements (O(n)).
Click to reveal answer
intermediate
What is the time complexity of removing the smallest element from a heap versus a sorted array?
Removing the smallest element from a heap takes O(log n) time due to re-heapifying, while in a sorted array it takes O(n) because elements must be shifted.
Click to reveal answer
intermediate
Explain why heaps are preferred for priority queue implementations over sorted arrays.
Heaps allow fast insertion and removal of the highest priority element (both O(log n)), while sorted arrays have slow insertion (O(n)) and removal (O(n)) due to shifting elements.
Click to reveal answer
What is the main advantage of a heap over a sorted array for insertion?
✗ Incorrect
Heaps allow insertion in O(log n) time without shifting many elements, unlike sorted arrays which require O(n) time.
Which operation is faster in a heap compared to an unsorted array?
✗ Incorrect
Heaps keep the smallest (or largest) element at the root, so it can be found in O(1) time.
What is the time complexity of removing the smallest element from a sorted array?
✗ Incorrect
Removing the smallest element requires shifting all other elements, which takes O(n) time.
Why is a heap better suited for priority queues than a sorted array?
✗ Incorrect
Heaps provide O(log n) insertion and removal, making them efficient for priority queues.
Which of these is NOT a reason why sorted arrays are inefficient for dynamic data?
✗ Incorrect
Finding the smallest element in a sorted array is fast (O(1)), but insertion and removal are slow due to shifting.
Explain why heaps exist and what problems they solve that sorted arrays cannot handle efficiently.
Think about how insertion and removal work differently in heaps and sorted arrays.
You got /4 concepts.
Describe the inefficiencies of sorted arrays when used for dynamic data where elements are frequently added or removed.
Focus on the cost of maintaining order in sorted arrays.
You got /4 concepts.