Data Structures & AlgorithmsVery hardTCS 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 = 4-th largest element of [53, 70, 9, 80, 6, 13, 33, 35, 69]?
- A. 80
- B. 33
- C. 53
- D. 35
Correct answer: C. 53
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 53 here. The k-th *smallest*, 33, is a different (sorted-ascending) quantity.
More Coding Interview Problems questions
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- Fractional knapsack, capacity 14 (items can be split). Items (weight, value): (8, 3), (6, 4), (4, 7), (3, 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…
- What is the length of the longest common subsequence of "EBBDBAD" and "ACBCBECC"?
- 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 "BDCEADE" and "EEABCADA"?