Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
Which structure finds the k-th largest element of a data stream after each insertion with O(log k) time per element and O(k) space?
- A. A min-heap of size k holding the k largest values seen so far
- B. A max-heap holding all elements
- C. A sorted array rebuilt on every insertion
- D. A hash set of all elements
Correct answer: A. A min-heap of size k holding the k largest values seen so far
A min-heap capped at size k keeps the k largest values; its root is the k-th largest element.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Which method checks in O(n) whether two strings are isomorphic (a one-to-one character mapping turns one into the other)?
- Which technique counts the inversions of an array of n distinct integers in O(n log n) time?
- How many contiguous subarrays of [2, 4, 6, 3, 9] have an odd sum?
- What is the length of the longest proper border of the string aabaaab (the longest proper prefix that is also a suffix)?
- Which algorithm finds all occurrences of a pattern in a text in expected linear time using a rolling hash of each window?
- You must perform exactly 3 operations on the array [4, -2, 5, -7, 3, -1]; each operation replaces one element by its negation (the same…