Track Heavy-Hitter ASINs in a Click Stream With Bounded Memory
Last updated: November 29, 2026
Quick Overview
A click stream of product IDs (ASINs) is too large to count exactly. Using at most k - 1 counters, return every ASIN that appears more than n / k times in a stream of length n.
Amazon
November 29, 20260
0
0 solved
A click stream of product IDs (ASINs) is too large to count exactly. Using at most k - 1 counters, return every ASIN that appears more than n / k times in a stream of length n.
Trending and abuse detection on Amazon need frequent items from enormous streams with tiny memory. The interviewer checks that you know the Misra-Gries summary, can argue why it never misses a true heavy hitter, and that a second pass (or a sketch) is needed to remove false positives.
What the Interviewer Expects
- Maintain at most k - 1 candidate counters
- Increment a matching counter, create one if space allows, otherwise decrement all
- Argue each decrement round removes k distinct occurrences, bounding undercount by n / k
- Verify candidates with a second pass to remove false positives
- Compare with Count-Min Sketch and the k = 2 Boyer-Moore majority vote
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
- Prove that a true heavy hitter can never be evicted.
- How would you merge Misra-Gries summaries from multiple shards?
- How would you handle a sliding window of the last hour instead of the whole stream?
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: an iterable of ASINs that can be read twice (for example a log file) and integer k >= 2. Output: the set of ASINs with frequency strictly greater than n / k. Memory for the first pass must be O...
High-Level Design
First pass, Misra-Gries: keep a dict of at most k - 1 counters. For each item, if it has a counter, increment it; else if fewer than k - 1 counters exist, add it with count 1; else decrement every cou...