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?

  1. A. O(n) for insert/extract-min; O(n) to build
  2. B. O(1) for insert/extract-min; O(n log n) to build
  3. C. O(log n) for insert/extract-min; O(n log n) to build, always
  4. 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

Exams that ask this topic