LeetCode 703 ·
Easy
Kth Largest Element in a Stream
Return the kth largest element after each value is added.
Try it
Step through the core mechanic. The simulator below runs the heap shape this problem is built on.
Walk the pattern
No dedicated step-through for this one yet. The shape is Heap — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.
The approach
Keep a min-heap of size k. The heap top is the kth largest. Each add pushes then pops if the size exceeds k, so updates are O(log k).
| Aspect | Value |
|---|---|
| Pattern | Heap |
| Recognise it by | Running kth largest as values stream in. |
| Time complexity | O(log k) |
| Space complexity | O(k) |
| Difficulty | Easy |
Who asks it
Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.