Mid Heap / Bucket Sort DSA & Coding Interviews

How do you find Top K Frequent Elements?

Short answer: Count frequencies with a hash map. Then either use a min-heap of size k (O(n log k)) or bucket sort by frequency (O(n)) since frequencies are in 1..n.

Complexity

Heap O(n log k); bucket O(n).

Common follow-ups

  • Top K frequent words (tie-break lexicographically)
  • Kth largest element
Prefer bucket sort when you want to show O(n) mastery.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details