Top K Frequent Elements

Algorithms
Medium
Uber
99.4K views

Given a non-empty array of integers, return the $k$ most frequent elements. The time complexity must be better than $O(n \log n)$. Use a Min-Heap or Bucket Sort.

Why Interviewers Ask This

Uber interviewers ask this to evaluate your ability to optimize for performance-critical systems like real-time ride matching or surge pricing. They specifically test if you can move beyond naive sorting solutions to achieve better than O(n log n) complexity using advanced data structures like heaps or bucket sort, demonstrating deep algorithmic efficiency.

How to Answer This Question

1. Clarify constraints: Confirm input size and whether k is small relative to n. 2. Propose a frequency map: Explain using a hash map to count occurrences in O(n) time. 3. Select the optimal structure: Choose a Min-Heap of size k for O(n log k) or Bucket Sort for O(n) depending on value range. 4. Walk through logic: Detail how you push/pop elements or fill buckets without full sorting. 5. Analyze complexity: Explicitly state why this beats O(n log n) and discuss space trade-offs. This structured approach mirrors Uber's focus on scalable, efficient backend solutions.

Key Points to Cover

  • Explicitly stating the O(n log k) or O(n) complexity to prove optimization skills
  • Demonstrating understanding of why standard sorting fails the specific constraint
  • Choosing between Min-Heap and Bucket Sort based on input characteristics
  • Explaining the trade-off between time complexity and space usage clearly
  • Connecting the algorithm choice to real-world scalability needs

Sample Answer

To solve the Top K Frequent Elements problem efficiently, I would first clarify that we need an algorithm faster than standard sorting. My approach starts with building a frequency map using a hash table, which takes O(n) time to count every element's occurrence. Once we have frequencies, the challenge is extracting the top k items without sorting the entire array. I propose using a Min-Heap of size k. We iterate through our frequency map; for each element, we push it onto the heap. If the heap exceeds size k, we remove the root, which holds the smallest frequency among the current top candidates. This ensures the heap always contains the k most frequent elements seen so far. The total time complexity becomes O(n + (n-k) log k), which simplifies to O(n log k). Since k is typically much smaller than n, this is significantly better than the O(n log n) required by sorting. Alternatively, if the range of numbers is limited, Bucket Sort could achieve true O(n) by placing elements into buckets based on their frequency counts. This solution demonstrates my ability to optimize algorithms for high-throughput environments like Uber's ride-matching system where latency matters.

Common Mistakes to Avoid

  • Solving via simple sorting which results in O(n log n) and violates the core constraint
  • Ignoring edge cases like when k equals the array length or k is zero
  • Failing to explain the step-by-step logic of how the heap maintains the top k elements
  • Not discussing the space complexity implications of the chosen data structure

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 57 Uber questions