Data Structures & AlgorithmsHardIBM · Deloitte 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 [37, 27, 83, 11, 31, 35, 23, 30]?
- A. 35
- B. 23
- C. 83
- D. 37
Correct answer: D. 37
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 37 here. The k-th *smallest*, 23, is a different (sorted-ascending) quantity.
More Coding Interview Problems questions
- What is the length of the longest common subsequence of "BECCBACE" and "AEDDEA"?
- 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…
- What is the length of the longest common subsequence of "BBBEEBBA" and "BCDDDC"?
- What is the length of the longest common subsequence of "DCDCDAC" and "AABDCD"?
- What is the length of the longest common subsequence of "BAECBABDC" and "EEAEDAB"?