Why do we need a heap when we already have sorted arrays?
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 linear time (O(n)).
Click to reveal answer
intermediate
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 accessed in constant time (O(1)), unlike an unsorted array which requires scanning all elements.
Click to reveal answer
intermediate
What operation is slow in a sorted array but fast in a heap?
Deleting the smallest or largest element is slow in a sorted array because it requires shifting elements, but fast in a heap due to its tree structure and heap property.
Click to reveal answer
intermediate
Explain why heaps are preferred for priority queues over sorted arrays.
Heaps allow both insertion and removal of the highest priority element in logarithmic time (O(log n)), while sorted arrays have slow insertions and deletions due to shifting elements.
Click to reveal answer
What is the time complexity of inserting an element into a sorted array?
AO(log n)
BO(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 quick access to the smallest element?
ALinked list
BUnsorted array
CStack
DHeap
✗ Incorrect
A heap keeps the smallest element at the root, allowing O(1) access.
Why is deleting the smallest element slow in a sorted array?
ABecause arrays do not support deletion
BBecause the smallest element is not at a fixed position
CBecause it requires shifting all other elements
DBecause it requires searching the entire array
✗ Incorrect
Deleting the smallest element requires shifting all remaining elements to fill the gap, which is slow.
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) due to reheapification.
Which operation is more efficient in a heap compared to a sorted array?
AInsertion
BAccessing any element
CLinear search
DSorting
✗ Incorrect
Insertion is more efficient in a heap (O(log n)) than in a sorted array (O(n)).
Explain why heaps exist and what problems they solve that sorted arrays cannot handle efficiently.
Think about the cost of shifting elements in arrays versus tree structure in heaps.
You got /4 concepts.
Describe the inefficiencies of sorted arrays when used for dynamic priority operations and how heaps improve on them.
Focus on insertion and deletion operations.
You got /4 concepts.
Practice
(1/5)
1. What operation does a sorted array perform poorly compared to a heap when elements are frequently inserted?
medium
A. Finding the minimum element
B. Maintaining sorted order after insertion - O(n) shift vs O(log n) heap push
C. Deleting an element by value
D. Printing all elements in order
Solution
Step 1: Sorted array insert
Inserting into a sorted array requires finding the position (O(log n) binary search) then shifting all elements to the right (O(n)). Total: O(n) per insert.
Step 2: Heap insert
Heap push in Rust's BinaryHeap: O(log n) - add at end, sift up. No shifting needed.
Final Answer:
Sorted array insert = O(n) due to shifting. Heap insert = O(log n). -> Option B
Thinking second largest is O(1) in heap (it's not)
5. Which algorithm specifically requires a heap (priority queue) and cannot be efficiently implemented with a sorted array due to dynamic insertions?
hard
A. Binary search
B. Merge sort
C. Dijkstra's shortest path algorithm
D. Quick sort
Solution
Step 1: Dijkstra's requirements
Dijkstra's algorithm repeatedly extracts the node with minimum distance AND inserts newly discovered nodes. This mix of extract-min and insert is exactly what a heap optimizes.
Step 2: Sorted array Dijkstra
Using a sorted array: extract-min O(1), but insert O(n) - total O(V²). With a binary heap: both O(log V) - total O((V+E) log V).