Skip to content
AI360Xpert
Beta
Difficulty: EasyHeap / Priority Queue

Kth Largest Element in a Stream

Problem in Plain English

Design a class that starts with an integer k and an initial list of numbers, then reports the kth largest value in the stream after every new arrival. Duplicates count as separate entries rather than distinct values.

Problem Statement

Design a class to find the kth largest element in a stream of numbers, where ranking follows sorted order and not distinct values.

Implement KthLargest(k, nums), which stores k and ingests the initial nums, and int add(val), which appends val to the stream and returns the element that is currently the kth largest.

Constraints

  • 1 <= k <= nums.length + 1
  • 0 <= nums.length <= 10^4
  • -10^4 <= nums[i] <= 10^4
  • -10^4 <= val <= 10^4
  • At most 10^4 calls will be made to add.
  • There will be at least k elements in the stream when asking for the kth element.

Examples

Example 1

Input
["KthLargest","add","add","add","add","add"] [[3,[4,5,8,2]],[3],[5],[10],[9],[4]]
Output
[null,4,5,5,8,8]

The constructor returns null. Initially the largest three entries are [8,5,4]. After arrivals 3,5,10,9,4, the top-three lists are [8,5,4], [8,5,5], [10,8,5], [10,9,8], [10,9,8], giving cutoffs 4,5,5,8,8. Both copies of 5 count.

Example 2

Input
["KthLargest","add","add","add"] [[1,[]],[-3],[-2],[-4]]
Output
[null,-3,-2,-2]

With k = 1 the class tracks the maximum, moving from -3 to -2 and staying at -2.

Example 3

Input
["KthLargest","add","add","add"] [[3,[-2,-2]],[-3],[-2],[0]]
Output
[null,-3,-2,-2]

The constructor has only k minus one entries, so no kth value is requested yet. Adding -3 makes [-2,-2,-3], whose third entry is -3. Adding another -2 makes the top three [-2,-2,-2]; adding 0 leaves [0,-2,-2]. Repeated negative values occupy separate ranks.

Intuition

The kth largest is the smallest member of the top k entries, counting duplicates separately. In Example 1, the top three initially are [8,5,4], so the cutoff is 4. A new 3 stays below that cutoff, while a new 5 replaces 4. Once a value falls outside the top k, future arrivals cannot improve its rank, so a streaming solution can discard it permanently.

Approaches

Sort Each Time

Solution Details

Reveal Sort Each Time: intuition, complexity, and code

Hints

Hint 1
Values outside the current top k can never become the answer, so there is no need to keep them.
Hint 2
A min-heap of the survivors puts the smallest survivor, which is the kth largest overall, at the root.
Hint 3
Push each arrival and pop the root whenever the heap grows past k entries.

Edge Cases

  • An empty initial array requires k = 1 under the published bounds; the first arrival is the running maximum.
  • The initial array may hold k minus one entries. Build normally, but only requested add calls have a valid kth result.
  • For k = 1, retain the maximum, even when all values are negative.
  • Duplicates count separately: three copies of -2 make the third largest -2.
  • An arrival equal to or below the current root can be inserted then evicted without changing the answer.

Common Mistakes and Interview Tips

  • Using a max-heap capped at k: removing its root discards the best entry instead of the worst survivor.
  • Using a set to eliminate duplicates changes the ranking required by the question.
  • Returning a constructor-time root as a kth answer before k entries exist; construction does not request an answer.
  • Removing when size equals k leaves only k minus one survivors; trim only on size greater than k.
  • Sorting the caller's nums array in place creates unexpected mutation; the sorting approach must copy it.
  • Claiming strict O(log k) for every dynamic-array insertion overlooks occasional O(k) resizing and the k = 1 boundary.

Key Takeaway

For the kth largest of a stream, cap a min-heap at k entries so the root always marks the cutoff between the top k and everything beneath.