Data Structures & AlgorithmsHardICPC regional · Product Company SDE
Problem: A min-heap (binary heap) supports insert and extract-min in what time complexity, and what is the time to build a heap from n arbitrary elements?
- A. O(n) for insert/extract-min; O(n) to build
- B. O(1) for insert/extract-min; O(n log n) to build
- C. O(log n) for insert/extract-min; O(n log n) to build, always
- D. O(log n) for insert/extract-min; O(n) to build the whole heap from an unsorted array (heapify)
Correct answer: D. O(log n) for insert/extract-min; O(n) to build the whole heap from an unsorted array (heapify)
Each insert/extract-min trickles an element up/down the heap's O(log n) height; building a heap bottom-up (heapify) does more work near the leaves and less near the root, summing to O(n) overall rather than n * O(log n).
More Coding Interview Problems questions
- Problem: To find the k largest elements among n numbers, which approach is asymptotically best when k is much smaller than n?
- Problem: An LRU (Least Recently Used) cache with O(1) get and put operations is classically implemented with which combination of data…
- Problem: Which statement about Kruskal's algorithm's overall time complexity is correct for a graph with V vertices and E edges?
- Problem: Topological sort is only defined for which kind of graph, and what happens if Kahn's algorithm is run on a graph that violates…
- Problem: Which best explains why merge sort is preferred over quicksort for sorting linked lists (as opposed to arrays)?
- Problem: Which best explains the time complexity difference between recursive (top-down with memoization) and iterative (bottom-up…