Rolling Top Sellers For A Live Subsidy Event
Last updated: November 29, 2026
Quick Overview
Design a class that ingests sale events (timestamp, product id, quantity) and answers queries for the k best-selling products over the last W seconds, breaking ties by product id.
Pinduoduo
November 29, 20260
0
0 solved
Design a class that ingests sale events (timestamp, product id, quantity) and answers queries for the k best-selling products over the last W seconds, breaking ties by product id.
During large subsidy campaigns Pinduoduo surfaces real-time best-seller lists to drive urgency, and the same windowed aggregation powers merchant dashboards and anomaly alerts. The interviewer checks sliding-window bookkeeping, correct expiry boundaries, and whether you can discuss the cost of each query versus each update.
What the Interviewer Expects
- Uses a FIFO queue of events plus a running count map
- Expires events lazily on both writes and reads
- Defines the window boundary precisely
- Uses a heap or partial sort for top k instead of a full sort when k is small
- Explains how this would be sharded and approximated at platform scale
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
- Events can arrive a few seconds late. How do you keep the counts correct?
- Top-k is queried far more often than events arrive. How do you change the trade-off?
- How would you do this across thousands of servers with a Count-Min Sketch or per-shard heaps?
- How would you exclude orders later flagged as fraudulent?
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, product_id, qty) with non-decreasing timestamps, and top_k(now, k) returning up to k product ids ranked by units sold in (now - W, now], ties by id ascending. Events older than the wi...
High-Level Design
Store events in a deque in arrival order and keep sold[pid] as the running total for the window. Before any operation, pop from the left while the oldest event is at or before now - W, subtracting...