Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Using a min-heap of size k (push each element, pop when size exceeds k, so the heap always holds the k largest seen so far), what is the k = 3-th largest element of [85, 4, 26, 47, 19, 40, 33, 32, 89]?

  1. A. 89
  2. B. 26
  3. C. 40
  4. D. 47

Correct answer: D. 47

Maintaining a min-heap capped at size k keeps exactly the k largest elements seen so far, with the smallest of those at the top; after processing the whole array, that top element is the k-th largest, which is 47 here. The k-th *smallest*, 26, is a different (sorted-ascending) quantity.

More Coding Interview Problems questions

Exams that ask this topic