How do you find the top-K elements from a large stream efficiently?
Reviewed by Gurusankar M.
Short Answer
Keep a min-heap (PriorityQueue) of size K: push each element, and when size exceeds K pop the smallest. You retain the K largest in O(n log K) time and O(K) space — far better than sorting everything (O(n log n)).
On this page
Concept Made Simple
Coffee Chat Question
“How do you find the top-K elements from a large stream efficiently?”
Remember It Faster
Mind Map Answer
For top-K largest, use a min-heap of size K: the smallest of your current best-K sits at the top, ready to be evicted the moment something larger arrives.
Learn by Doing
Hands-on Keyboard
PriorityQueue<Integer> heap = new PriorityQueue<>(); // min-heap
for (int x : stream) {
heap.offer(x);
if (heap.size() > k) heap.poll(); // drop smallest
}
// heap now holds the k largestk largest elements
- Time:
- O(n log k)
- Space:
- O(k)
Think Beyond the Expected
What If?
Why a min-heap for top-K largest, not a max-heap?
A size-K min-heap keeps the smallest of your current top-K at the root, so you can evict it in O(log K) when a larger element arrives — using only O(K) memory. A max-heap of all n elements needs O(n) space and gains nothing.
Real World
Top-N dashboards (highest-value orders, slowest endpoints, top customers) over huge datasets use a bounded heap so you never load or sort the whole dataset in memory.
Interviewer's Expectation
Keywords they're listening for:
- min-heap of size K
- O(n log k) vs O(n log n)
- O(k) space
- bounded memory
Common Mistakes
- Sorting the entire dataset to take K
- Using a max-heap of all n elements (O(n) space)
- Wrong heap direction (min vs max)
Best Practices
- Bound the heap to K for streaming top-K
- Use a Comparator for domain objects
- Consider QuickSelect for one-shot in-memory top-K
Follow-up Questions
- How does the heap-based approach compare to full sort or QuickSelect?
- How would you parallelize top-K across shards?
- What Comparator do you use for top-K by a custom field?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Performance (Java Collections) Interview question: "How do you find the top-K elements from a large stream efficiently?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair