Data Structures & AlgorithmsVery hardProduct 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 = 2-th largest element of [18, 9, 24, 27, 89, 4, 90, 37, 58, 78]?
- A. 89
- B. 90
- C. 9
- D. 78
Correct answer: A. 89
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 89 here. The k-th *smallest*, 9, is a different (sorted-ascending) quantity.
More Coding Interview Problems questions
- Problem: 8 servers are joined by cables one at a time. Count the connected groups at the end and how many cables were redundant (joined…
- Merging overlapping intervals (sort by start, merge when the next interval's start is ≤ the current merged interval's end — touching…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- Problem: 7 servers are joined by cables one at a time. Count the connected groups at the end and how many cables were redundant (joined…
- 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…
- What is the length of the longest common subsequence of "ECAAEEDAE" and "BEEDEDD"?