Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
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 [29, 40, 80, 58, 23, 18, 26, 50, 81, 47, 6]?
- A. 80
- B. 58
- C. 81
- D. 18
Correct answer: A. 80
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 80 here. The k-th *smallest*, 18, is a different (sorted-ascending) quantity.
More Coding Interview Problems questions
- Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (7, 7), (5, 7), (3, 14), (5, 11), (6, 5). Using the greedy…
- What is the length of the longest common subsequence of "BEADCBBDA" and "BAAEDC"?
- A divide-and-conquer routine satisfies T(n) = 8T(n/2) + n². What is its running time (by the master theorem, extended for polylog terms…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- What is the length of the longest common subsequence of "CBEDDDE" and "BEDECCBB"?
- Fractional knapsack, capacity 13 (items can be split). Items (weight, value): (8, 8), (3, 4), (6, 13), (8, 10), (6, 6). Using the greedy…