Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: Which of these is the correct worst-case time complexity of building a max-heap from an array of n unsorted elements using the standard bottom-up heapify approach?

  1. A. O(n log n)
  2. B. O(log n)
  3. C. O(n^2)
  4. D. O(n)

Correct answer: D. O(n)

Although each of the roughly n/2 internal nodes could sift down O(log n) levels, most nodes are near the bottom of the heap and sift down only a little; summing the work across all levels gives a total of O(n), not O(n log n).

More Coding Interview Problems questions

Exams that ask this topic