Answer: eBay OA Question 2024 June | Heap & Sorting | on-campus
Answer · Posted Jun 2026
Solution: Top K Frequent Elements — Bucket Sort (O(N)) Approach We use Bucket Sort for an optimal O(N) solution. The key insight: the frequency of any element is at most N (if all elements are the same). So we create N+1 buckets where bucket[i] holds all elements with frequency i. Count frequencies of all elements using a HashMap. Place each element in the bucket corresponding to its frequency. Read from the highest-frequency bucket down, collecting elements until we have K ...
The full answer & interview discussion are available to premium members.
Log in Create a free account