81. Top K Frequent Elements
mediumAsked at RedditReturn the k most frequent elements in an array. Reddit uses this to test count + heap design — the exact shape of their trending-words detector in subreddit titles over a time window.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit ranking-team on-site favorite.
- Blind (2025-12)— Reported as Reddit trending-detection canonical.
Problem
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
Constraints
1 <= nums.length <= 10^5-10^4 <= nums[i] <= 10^4k is in the range [1, the number of unique elements in the array].It is guaranteed that the answer is unique.
Examples
Example 1
nums = [1,1,1,2,2,3], k = 2[1,2]Example 2
nums = [1], k = 1[1]Approaches
1. Sort entries by count
Count occurrences; sort entries by count descending; return top k.
- Time
- O(n + m log m)
- Space
- O(m)
function topKFrequent(nums, k) {
const count = new Map();
for (const n of nums) count.set(n, (count.get(n) || 0) + 1);
return [...count.entries()].sort((a, b) => b[1] - a[1]).slice(0, k).map(e => e[0]);
}Tradeoff: O(m log m) sort. m = unique count.
2. Bucket sort by frequency (optimal)
Count; create n+1 buckets indexed by frequency. Walk buckets high to low; collect first k.
- Time
- O(n)
- Space
- O(n)
function topKFrequent(nums, k) {
const count = new Map();
for (const n of nums) count.set(n, (count.get(n) || 0) + 1);
const buckets = Array.from({length: nums.length + 1}, () => []);
for (const [val, c] of count) buckets[c].push(val);
const out = [];
for (let i = buckets.length - 1; i >= 0 && out.length < k; i--) {
for (const v of buckets[i]) {
if (out.length < k) out.push(v);
}
}
return out;
}Tradeoff: Linear. Bucket sort exploits the bounded frequency range (0..n).
Reddit-specific tips
Reddit interviewers will accept any version but reward the bucket-sort O(n). Bonus signal: connect to their trending-keywords pipeline where bucket sort beats heap when n is large but k is moderate.
Common mistakes
- Allocating n buckets but indexing into a count up to n inclusive (off by one).
- Adding all bucket contents (must respect k).
- Using heap of size n (O(n log n)); use heap of size k for O(n log k).
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Sort characters by frequency (LC 451).
- Top K Frequent Words (LC 692).
- Kth largest element (LC 215).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Heap vs. bucket sort?
Heap O(n log k); bucket O(n). Bucket wins when frequencies have a known bounded range (which is always n here).
What if multiple values tie for k-th?
Problem guarantees unique answer, so this can't happen.
Practice these live with InterviewChamp.AI
Drill Top K Frequent Elements and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →