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 [58, 19, 4, 22, 15, 10, 79, 34, 77, 18, 14]?
- A. 10
- B. 58
- C. 79
- D. 77
Correct answer: D. 77
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 77 here. The k-th *smallest*, 10, is a different (sorted-ascending) quantity.
More Coding Interview Problems questions
- Using O(n²) DP (dp[i] = 1 + max(dp[j]) over all j < i with a[j] < a[i]), what is the length of the longest STRICTLY increasing subsequence…
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- 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 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"?