Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style 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 = 2-th largest element of [13, 51, 45, 5, 85, 89, 25, 36, 75]?
- A. 89
- B. 75
- C. 85
- D. 13
Correct answer: C. 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*, 13, is a different (sorted-ascending) quantity.
More Coding Interview Problems questions
- 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…
- Fractional knapsack, capacity 17 (items can be split). Items (weight, value): (4, 14), (7, 5), (5, 4), (5, 10). Using the greedy…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- 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…
- 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 15 (items can be split). Items (weight, value): (3, 15), (7, 15), (4, 15), (5, 8), (3, 11). Using the greedy…