Top K Hashtags Over A Sliding Ten Minute Window
Last updated: November 29, 2026
Quick Overview
Implement a class that ingests (timestamp, hashtag) events in time order and answers top(now, k): the k most frequent hashtags in the last 10 minutes, case-insensitive, with ties broken alphabetically.
X
November 29, 20260
0
0 solved
Implement a class that ingests (timestamp, hashtag) events in time order and answers top(now, k): the k most frequent hashtags in the last 10 minutes, case-insensitive, with ties broken alphabetically.
Trends on X are driven by what is spiking right now, so counts must expire as time moves. This screen checks whether you can maintain a windowed counter incrementally instead of recounting, and reason about the cost of the top-k query.
What the Interviewer Expects
- Keeps a queue of events plus a running counter
- Evicts expired events lazily on both write and read
- Normalizes case before counting
- Gives a clear tie-break
- States the cost of top-k and how to improve it
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
- At 500,000 events per second a deque of every event is too large. How do you bucket by second?
- Raw counts favour always-popular tags. How would you score a spike relative to a baseline?
- How would you shard this across machines and merge results?
- How do you stop a bot network from manufacturing a trend?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
record(ts, tag) with non-decreasing ts in seconds; top(now, k) returns [(tag, count)] for events with now - 600 < ts <= now. Tags are compared lowercase. Ties go to the alphabetically smalle...
High-Level Design
Store events in a deque in arrival order and a Counter of tag to count inside the window. Before any operation, pop from the left while the oldest event is at or before now - window, decrementing ...