Data Structures & AlgorithmsVery hardGoogle · Flipkart interview

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 = 5-th largest element of [89, 66, 31, 80, 85, 12, 65, 14, 38, 13, 56]?

  1. A. 89
  2. B. 65
  3. C. 38
  4. D. 56

Correct answer: B. 65

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

More Coding Interview Problems questions

Exams that ask this topic