Loading section...

Bucket Sort: O(n) Top-K That Interviewers Love

Concepts: pyBucketSortTopK, pyLinearFrequency

Here is the thing about bucket sort for top-K: the interviewer is not testing whether you memorized the algorithm. They are testing whether you can look at a constraint (frequencies are bounded by n) and exploit it to break through the O(n log n) sorting barrier. Candidates who find this insight on their own, in real time, score significantly higher than candidates who present the O(n log n) solution and stop. The bucket sort idea is elegant, explainable in 30 seconds, and shows genuine algorithmic thinking. The Key Insight Walk through the example with the interviewer. [1,1,1,2,2,3], k=2. Counts: {1:3, 2:2, 3:1}. Buckets: buckets[3]=[1], buckets[2]=[2], buckets[1]=[3]. Scan right to left: freq=3, add 1. freq=2, add 2. len(result)==k=2, return [1,2]. Done. O(n) time, O(n) space. How to Pre