Medium👤 3-5 years👤 8-15 years 1 min read

How do you find the top-K elements from a large stream efficiently?

Asked inAmazonGoogleMicrosoft
#priorityqueue#heap#top-k#coding#complexity
Report issue

⚡ 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)).

Coffee Chat Question

Concept Made Simple

How do you find the top-K elements from a large stream efficiently?

🧠Mind Map Answer

Remember It Faster

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.

⌨️Hands-on Keyboard

Learn by Doing

java
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 largest
Output
k largest elements
⏱️ Time: O(n log k)💾 Space: O(k)

🔥What If?

Think Beyond the Expected

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 KO(n log k) vs O(n log n)O(k) spacebounded 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

  • 1How does the heap-based approach compare to full sort or QuickSelect?
  • 2How would you parallelize top-K across shards?
  • 3What Comparator do you use for top-K by a custom field?

🧩Related Technologies

PriorityQueueComparatorQuickSelectStream

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions