Why do we need a heap instead of just using 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 for these operations.
Click to reveal answer
beginner
What is the main inefficiency of a sorted array when frequently inserting elements?
Inserting an element in a sorted array requires shifting elements to keep the order, which takes O(n) time, making it slow for many insertions.
Click to reveal answer
intermediate
How does a heap improve the efficiency of finding the smallest or largest element?
A heap keeps the smallest or largest element at the root, so accessing it is O(1), and insertion or removal takes O(log n), which is faster than a sorted array's O(n) insertion.
Click to reveal answer
intermediate
What operation is costly in a sorted array but efficient in a heap?
Removing the smallest or largest element is costly in a sorted array because it requires shifting elements, but in a heap, it is efficient due to the heap property and restructuring in O(log n).
Click to reveal answer
advanced
Explain the trade-off between heaps and sorted arrays.
Sorted arrays provide fast access to any element by index but slow insertions and deletions. Heaps provide fast access to the smallest or largest element and efficient insertions and deletions but do not support fast random access.
Click to reveal answer
What is the time complexity of inserting an element into a sorted array?
AO(log n)
BO(n log n)
CO(1)
DO(n)
✗ Incorrect
Inserting into a sorted array requires shifting elements to maintain order, which takes O(n) time.
Which data structure allows O(1) access to the smallest element?
ASorted array
BUnsorted array
CHeap
DLinked list
✗ Incorrect
A heap keeps the smallest element at the root, allowing O(1) access.
Why is removing the smallest element from a sorted array inefficient?
ABecause it requires shifting all other elements
BBecause the smallest element is not at a fixed position
CBecause arrays do not support removal
DBecause it requires searching the entire array
✗ Incorrect
Removing the smallest element requires shifting all subsequent elements to fill the gap, which is O(n).
What is the time complexity of inserting an element into a heap?
AO(1)
BO(log n)
CO(n)
DO(n log n)
✗ Incorrect
Insertion in a heap requires bubbling up the element, which takes O(log n) time.
Which data structure is better for frequent insertions and deletions of the smallest element?
AHeap
BSorted array
CUnsorted array
DStack
✗ Incorrect
Heap supports efficient insertions and deletions of the smallest element in O(log n) time.
Explain why a heap is preferred over a sorted array for priority queue operations.
Think about how insertion and removal work in both data structures.
You got /4 concepts.
Describe the main inefficiency of sorted arrays when used for dynamic data with frequent insertions and deletions.
Consider what happens when you add or remove elements in the middle.
You got /4 concepts.
Practice
(1/5)
1. Why do we use a heap instead of just a sorted array for priority queue operations?
easy
A. Because heaps do not allow duplicate elements
B. Because heaps store elements in sorted order permanently
C. Because heaps allow faster insertion and deletion of the highest priority element
D. Because heaps use less memory than arrays
Solution
Step 1: Understand insertion and deletion in sorted arrays
Inserting or deleting an element in a sorted array requires shifting elements, which takes O(n) time.
Step 2: Compare with heap operations
Heaps allow insertion and deletion of the highest priority element in O(log n) time by maintaining a partial order.
Final Answer:
Because heaps allow faster insertion and deletion of the highest priority element -> Option C
Quick Check:
Heap operations faster than sorted array insert/delete [OK]
Hint: Heaps speed up insert/delete compared to sorted arrays [OK]
Common Mistakes:
Thinking heaps keep all elements fully sorted
Assuming heaps use less memory than arrays
Believing heaps disallow duplicates
2. Which of the following is the correct syntax to insert an element into a min-heap in C# using a PriorityQueue named pq?
easy
A. pq.Add(5);
B. pq.Enqueue(5, 5);
C. pq.Insert(5);
D. pq.Push(5);
Solution
Step 1: Recall PriorityQueue method for insertion
In C#, PriorityQueue uses Enqueue(item, priority) to insert elements.
Step 2: Check options for correct method and parameters
Only pq.Enqueue(5, 5); matches the correct syntax for insertion with priority.
Final Answer:
pq.Enqueue(5, 5); -> Option B
Quick Check:
Use Enqueue(item, priority) to insert in PriorityQueue [OK]
Hint: Use Enqueue(item, priority) to insert in C# PriorityQueue [OK]
Common Mistakes:
Using Insert or Add methods which don't exist for PriorityQueue
Forgetting to provide priority value
Using Push which is not a C# PriorityQueue method
3. What is the output of the following C# code snippet?
var pq = new PriorityQueue();
pq.Enqueue(10, 10);
pq.Enqueue(5, 5);
pq.Enqueue(20, 20);
int first = pq.Dequeue();
int second = pq.Dequeue();
Console.WriteLine($"{first} {second}");
medium
A. 5 10
B. 10 5
C. 20 10
D. 10 20
Solution
Step 1: Understand PriorityQueue behavior
In C#, PriorityQueue is a min-heap by default, so Dequeue returns elements with the smallest priority first.
Dequeue returns elements in ascending priority order [OK]
Hint: Dequeue returns smallest priority first in C# PriorityQueue [OK]
Common Mistakes:
Assuming dequeue returns highest priority first (max-heap)
Mixing up item and priority values
Thinking enqueue order determines dequeue order
4. The following code tries to remove the highest priority element from a sorted array but runs into performance issues. What is the main problem?
List<int> sortedList = new List<int>{1, 3, 5, 7, 9};
int highest = sortedList[sortedList.Count - 1];
sortedList.RemoveAt(sortedList.Count - 1);
medium
A. Removing the last element is O(1), but inserting is slow
B. Removing the last element in a list is O(n) and slow
C. Accessing the last element is O(n) and slow
D. The list is not sorted, so removal is incorrect
Solution
Step 1: Analyze removal operation on List
Removing the last element with RemoveAt(last index) is O(1) because no shifting is needed.
Step 2: Consider insertion cost in sorted list
Inserting new elements to keep list sorted requires shifting and is O(n), causing performance issues.
Final Answer:
Removing the last element is O(1), but inserting is slow -> Option A
Quick Check:
Removal at end fast; insertion in sorted list slow [OK]
Hint: Removal at end is fast; insertion in sorted list is slow [OK]
Common Mistakes:
Thinking removal at end is O(n)
Confusing access time with removal time
Assuming list is unsorted
5. You have a large stream of numbers arriving continuously. You want to always quickly find and remove the smallest number seen so far. Why is a heap better than maintaining a sorted array for this task?
hard
A. Because heaps do not allow duplicate numbers
B. Because heaps keep all elements fully sorted at all times
C. Because sorted arrays use more memory than heaps
D. Because heaps allow O(log n) insertion and removal, while sorted arrays require O(n) insertion
Solution
Step 1: Consider insertion cost in sorted arrays
Inserting a new number in a sorted array requires shifting elements, which is O(n).
Step 2: Compare with heap insertion and removal
Heaps insert and remove the smallest element in O(log n) time by maintaining partial order.
Step 3: Understand why heaps suit streams
For continuous streams, efficient insert and remove operations are critical, making heaps better.
Final Answer:
Because heaps allow O(log n) insertion and removal, while sorted arrays require O(n) insertion -> Option D