Top K Trending Pins Over a Sliding Time Window
Last updated: November 29, 2026
Quick Overview
Build a component that ingests save events (timestamp, pin_id) in time order and, at any moment, returns the k Pins with the most saves in the last W seconds, breaking ties by pin id.
0
0
0 solved
Build a component that ingests save events (timestamp, pin_id) in time order and, at any moment, returns the k Pins with the most saves in the last W seconds, breaking ties by pin id.
Trending surfaces on Pinterest, such as rising ideas in search and seasonal topics, need recent counts rather than all-time counts. The interviewer checks that you can expire old events correctly, choose a sensible structure for top-k queries, and reason about the trade-off between exact counts and sketches at Pinterest scale.
What the Interviewer Expects
- Keeps a deque of events and evicts those outside the window on every call
- Maintains per-Pin counts and removes zero entries
- Answers top-k with a bounded heap rather than a full sort
- States a precise window boundary (here, events with ts <= now - W expire)
- Discusses approximate alternatives like count-min sketch with a heap of heavy hitters
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
- How would you make top_k O(k) when queries are far more frequent than saves?
- Events can arrive up to 5 seconds late; how do you handle that?
- How would you shard this across many machines and merge results?
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, pin_id) with non-decreasing ts. top_k(now, k) with now >= last ts.
- Window is (now - W, now]: an event exactly W seconds old has expired.
- Ties broken by smaller pin id. Return fewer th...
High-Level Design
A deque holds events in time order, so expiry is just popping from the left while the head is too old. A Counter tracks live counts. For queries, heapq.nsmallest with key (-count, pin_id) returns the ...