Min-heap of size k, pretty standard once you see it.
Use a min-heap of size k to maintain the k largest elements, where the root is the k-th largest. For each add, push the new value and if the heap exceeds size k, pop the smallest; then return the root. This gives O(log k) time per insertion and O(k) space.
Pro tip: Mention that a max-heap of all elements would be O(n) per insertion, and a sorted list would be O(n) due to shifting; the min-heap of size k is optimal. Also, clarify that duplicates are handled naturally because the heap stores values, not unique elements.
Confirm that k is valid (1 ≤ k ≤ initial size + number of adds) and that duplicates are allowed. Discuss expected input sizes to justify the need for O(log k) per insertion.
Select a min-heap of size k. Explain that it keeps the k largest elements seen so far, with the smallest among them at the root, which is the k-th largest.
In the constructor, initialize the heap with the first k elements (or all if fewer), then for each remaining element, if it's larger than the root, replace the root and heapify. For add, push the value, and if size > k, pop the root; then return the root.
State that each add does at most one push and one pop, each O(log k), so O(log k) time. Space is O(k). Handle duplicates by allowing multiple equal values in the heap; they are treated as separate elements.
Walk through a small example, e.g., k=3, initial [4,5,8,2], add(3) returns 4, add(10) returns 5, etc., to demonstrate correctness and duplicate handling.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.