Top Trending Products Over a Sliding Ten Minute Window

Last updated: November 29, 2026

Quick Overview

Design a class that ingests product view events in timestamp order and answers queries for the k most viewed products in the last W seconds, breaking ties by product id.

Coupang
Coding & Algorithms
Software Engineer
Coupang
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Design a class that ingests product view events in timestamp order and answers queries for the k most viewed products in the last W seconds, breaking ties by product id.

Coupang surfaces 'trending now' products during events such as Gold Box deals, where popularity shifts within minutes. The interviewer checks that you can keep counts consistent under eviction and choose a query strategy whose cost you can defend.

What the Interviewer Expects
  • Keep a deque of events and a count map that are updated together
  • Evict events at or before now minus W, and delete zero counts
  • Use a size k heap or nsmallest rather than sorting all products
  • Explain the trade-off between O(1) ingest and O(U log k) queries
  • Clarify window boundary semantics (open or closed)
Key Topics to Cover
Sliding window
Deque
Heap
Streaming counts
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if events can arrive a few seconds out of order?
  • How do you scale this to many servers handling one shard of products each?
  • Memory is too high with every raw event; how would bucketing by second help?
  • How would you weight purchases more than views?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

record(ts, product_id) with non-decreasing timestamps and top_k(now, k) returning product ids ordered by count descending, then id ascending, for events with now - W < ts <= now. Queries are fre...

High-Level Design

A deque holds raw events in arrival order and a dictionary holds counts for the current window. Ingest appends and increments in O(1). Each query first evicts expired events from the left, decrementin...


Submit Your Answer
Markdown supported

Related Questions