Data Structures & AlgorithmsHardAmazon · Microsoft OA
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 [72, 62, 89, 29, 76, 35, 61, 46, 31]?
- A. 62
- B. 89
- C. 35
- D. 72
Correct answer: D. 72
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 72 here. The k-th *smallest*, 35, is a different (sorted-ascending) quantity.
More Coding Interview Problems questions
- What is the length of the longest common subsequence of "ECAAEEDAE" and "BEEDEDD"?
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- Fractional knapsack, capacity 16 (items can be split). Items (weight, value): (3, 8), (7, 6), (3, 8), (6, 8). Using the greedy…
- Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (5, 14), (6, 9), (2, 9), (5, 13). Using the greedy…
- Fractional knapsack, capacity 12 (items can be split). Items (weight, value): (3, 12), (6, 9), (3, 13), (5, 9), (5, 6). Using the greedy…
- 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…