Kth Largest Element in a Stream
Heap / Priority Queue
Asked at 9 companies
3 recent
- Amazon
- Microsoft
- Tinder
- Adobe
- Wells Fargo
- Meta
- Goldman Sachs
- Bloomberg
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Merge sort: split, sort, merge · Partitioning around a pivot
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Task schedulers and leaderboards: A heap always knows the most urgent job or the top score, so schedulers and “top 10” lists never re-sort everything.
About this problem
Keep the smallest or largest items on top: top-k, streams, and scheduling.
Read the full statement and examples on LeetCode, try it for 20–30 minutes, then come back for the walkthrough and the step-by-step visual.
Examples
Full statement on LeetCode ↗Example 1
- Input
- k = 3nums = [4, 5, 8, 2], then add 3, 5, 10
- Output
- [4, 5, 5]
Walk through it step by step(8 steps · shows the approach, so try first)
- Keep only the 3 biggest numbers seen, in a (smallest on top). The top is then exactly the 3th largest — anything smaller than it can never matter again.
- Add 4 to the heap.
- Add 5 to the heap.
- Add 8 to the heap.
- Add 2 to the heap. Now it holds 4 numbers, so drop the smallest (2).
- Add 3 to the heap. Now it holds 4 numbers, so drop the smallest (3). The top, 4, is the 3rd largest so far.
- Add 5 to the heap. Now it holds 4 numbers, so drop the smallest (4). The top, 5, is the 3rd largest so far.
- Add 10 to the heap. Now it holds 4 numbers, so drop the smallest (5). The top, 5, is the 3rd largest so far.
Code it on LeetCode
This one needs setup the editor here can't do yet (a class you build, linked nodes, or a helper LeetCode provides), so solve it on LeetCode, with its own tests. Then tick My code passed below, and it joins your reviews. With your LeetCode account linked, that happens on its own.
An error or a Wrong Answer there? What each one means.
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: optimal solution
Solution code
Optimal solutionKeep only the k largest numbers in a . Its smallest element (the top) is then the k-th largest overall; each add pushes and, if the heap grows past k, pops the smallest.
class KthLargest:
def __init__(self, k: int, nums: List[int]):
self.k = k
self.heap = []
for x in nums:
self.add(x)
def add(self, val: int) -> int:
heappush(self.heap, val)
if len(self.heap) > self.k:
heappop(self.heap)
return self.heap[0]Reference solution, tested in Python, Java and C++.
Other ways to solve it
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.