Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard

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 = 2-th largest element of [85, 13, 67, 49, 65, 59, 74, 30, 87]?

  1. A. 85
  2. B. 87
  3. C. 74
  4. D. 30

Correct answer: A. 85

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 85 here. The k-th *smallest*, 30, is a different (sorted-ascending) quantity.

More Coding Interview Problems questions

Exams that ask this topic