Top K Most Frequent Hashtags in a Post Batch
Last updated: November 29, 2026
Quick Overview
Given a list of hashtags extracted from a batch of posts and an integer k, return the k most frequent hashtags ordered by count descending, breaking ties alphabetically.
Meta
November 29, 20260
0
0 solved
Given a list of hashtags extracted from a batch of posts and an integer k, return the k most frequent hashtags ordered by count descending, breaking ties alphabetically.
Trending hashtags is a natural Meta framing for top-k frequency. Interviewers look for counting with a hash map, then a choice between a size-k heap, bucket sort, or full sort, with a clear explanation of the trade-offs and the tie-break.
What the Interviewer Expects
- Counts with a hash map in one pass
- Uses a heap of size k or bucket sort rather than sorting everything by default
- Handles the tie-break rule deterministically
- States complexity for each approach
- Discusses the streaming and distributed versions
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- The stream is unbounded; how do you approximate top k with bounded memory?
- How would you compute top k over a sliding one-hour window?
- How would you merge top-k results from 1,000 shards correctly?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of strings, k >= 1. If fewer than k distinct tags exist, return all of them. Order: higher count first, then lexicographically smaller tag. Aim for better than O(n log n) when k is small r...
High-Level Design
Count frequencies with Counter. Then pick the top k by key (-count, tag). heapq.nsmallest maintains a heap of size k internally, giving O(d log k) for d distinct tags. Bucket sort by count (inde...