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?
AAlways keeps elements sorted
BFaster insertion without shifting elements
CUses less memory
DAllows random access in constant time
✗ 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?
AFinding the smallest element
BAccessing an element by index
CSorting the entire data
DAppending at the end
✗ 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?
AO(n)
BO(log n)
CO(1)
DO(n log n)
✗ 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?
AHeaps allow faster random access
BHeaps use less memory
CHeaps allow faster insertion and removal of priority elements
DHeaps keep elements in sorted order
✗ 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?
AMaintaining sorted order is costly
BRemoval requires shifting elements
CInsertion requires shifting elements
DFinding the smallest element is slow
✗ 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.
Practice
(1/5)
1. Why do we use a heap instead of just a sorted array when we want to efficiently get the smallest or largest element?
easy
A. Because heaps allow faster insertion and deletion while keeping track of min or max elements.
B. Because sorted arrays use less memory than heaps.
C. Because heaps store elements in sorted order all the time.
D. Because sorted arrays can only store numbers, not other data types.
Solution
Step 1: Understand operations on sorted arrays
Sorted arrays allow quick access to smallest or largest element but inserting or deleting elements requires shifting many items, which is slow (O(n)).
Step 2: Compare with heap operations
Heaps keep track of min or max element and allow insertion and deletion in O(log n) time, which is faster for dynamic data.
Final Answer:
Because heaps allow faster insertion and deletion while keeping track of min or max elements. -> Option A
Quick Check:
Heap insertion/deletion faster than sorted array [OK]
Hint: Heaps speed up insert/delete, sorted arrays don't [OK]
Common Mistakes:
Thinking heaps keep all elements fully sorted
Believing sorted arrays are always faster
Confusing memory use with operation speed
2. Which of the following Go code snippets correctly creates a min-heap using the container/heap package?
easy
A. h := make([]int, 0); heap.Pop(h)
B. h := []int{}; heap.Push(h, 5)
C. h := &IntHeap{}; heap.Init(h)
D. h := new([]int); heap.Init(h)
Solution
Step 1: Check correct heap initialization
In Go, to use heap, you define a type implementing heap.Interface (like IntHeap), create its pointer, then call heap.Init.
Step 2: Analyze options
h := &IntHeap{}; heap.Init(h) correctly creates pointer to IntHeap and initializes it. Options B and C misuse slice without pointer or interface. h := new([]int); heap.Init(h) uses pointer to slice incorrectly.
Final Answer:
h := &IntHeap{}; heap.Init(h) -> Option C
Quick Check:
Proper heap init requires pointer to heap.Interface [OK]
Hint: Use pointer to heap struct and call heap.Init [OK]
Common Mistakes:
Using slice directly without pointer
Calling heap.Push before heap.Init
Using new([]int) instead of &IntHeap{}
3. What will be the output of the following Go code using a min-heap?
A. Heap is not initialized before pushing elements.
B. Push method does not convert interface{} to int.
C. Pop method removes the first element instead of last.
D. Less method uses < instead of >, so it behaves like min-heap.
Solution
Step 1: Check Less method for max-heap behavior
For max-heap, Less should return true if h[i] > h[j]. Here it uses h[i] < h[j], which creates min-heap behavior.
Step 2: Verify other methods and usage
Push and Pop methods are correct. Heap is initialized before push. So main issue is Less method logic.
Final Answer:
Less method uses < instead of >, so it behaves like min-heap. -> Option D
Quick Check:
Less defines heap order; wrong comparison causes wrong heap type [OK]
Hint: Max-heap needs Less to use >, not < [OK]
Common Mistakes:
Ignoring Less method logic
Thinking Push or Pop cause max-heap failure
Not initializing heap before use
5. You want to maintain a dynamic list of numbers where you frequently insert new numbers and need to quickly find and remove the smallest number. Why is a heap better than a sorted array for this task, and what is the time complexity difference?
hard
A. Heap allows insertion and removal in O(log n), while sorted array requires O(n) for insertion and removal.
B. Heap allows insertion in O(1) and removal in O(n), sorted array does both in O(log n).
C. Heap and sorted array both have O(n) insertion and removal times.
D. Heap requires more memory but has same time complexity as sorted array.
Solution
Step 1: Analyze insertion and removal in sorted array
Sorted array keeps elements sorted, so insertion requires shifting elements (O(n)) and removal also requires shifting (O(n)).
Step 2: Analyze insertion and removal in heap
Heap keeps partial order, allowing insertion and removal of smallest element in O(log n) time by adjusting tree structure.
Final Answer:
Heap allows insertion and removal in O(log n), while sorted array requires O(n) for insertion and removal. -> Option A