← Questions

Top-k clicked items over a rolling window

0
AmazonCodingLast asked Jul 2025

A coding interview question asked in Amazon's coding round, last asked Jul 2025. It tests sliding window, heap and hashing.

Click events arrive as a high-volume stream, each with an event id, a timestamp in milliseconds and a URL. Build a data structure that records events and answers, at any moment, which URLs were clicked most in a recent time window and how many distinct URLs were clicked. Results must be near real time, memory must stay bounded by evicting old events, events can arrive late by up to L milliseconds, and a repeated event id must not be counted twice.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • record(event_id: str, ts: int, url: str) -> None — ignores an event_id it has already seen, and events older than the lateness limit allows.
  • top_k(window_ms: int, k: int, now: int) -> list[str] — the k most clicked URLs in the half-open interval (now - window_ms, now], most clicks first, ties by URL ascending.
  • unique(window_ms: int, now: int) -> int — the number of distinct URLs in the same interval.

Example

record("e1", 1000, "/a")
record("e2", 1500, "/b")
record("e3", 2000, "/a")
record("e3", 2000, "/a")     # duplicate id, ignored
record("e4", 2500, "/c")
record("e5", 2600, "/b")
record("e6", 2700, "/b")
top_k(2000, 2, 3000)   ->  ["/b", "/a"]   # (1000, 3000]: /b 3, /a 1, /c 1; e1 at 1000 is outside
unique(2000, 3000)     ->  3
top_k(1000, 2, 3000)   ->  ["/b", "/c"]   # (2000, 3000]: /b 2, /c 1
unique(500, 3000)      ->  1              # (2500, 3000]: only /b

Constraints

  • Millions of events per minute; the largest window and the lateness L are known in advance.
  • Aim for O(log K) or better per event for keeping the top list, and memory proportional to the window, not the whole stream.

Clarifying questions

  • Is the window fixed for the structure, or can each query choose its own length?
  • How exact must the answer be: is approximate top-k acceptable at this volume?
  • How late can an event arrive, and what happens to one that is later than L?
  • How long must event ids be remembered for deduplication?
  • How many producers write at once, and do queries need a consistent snapshot?

What a strong answer covers

  • Time buckets (for example one per second) in a ring or deque, each with per-URL counts; a global count map updated when a bucket enters and leaves the window.
  • Top-k: a size-k heap rebuilt from the counts at query time, or an ordered structure (counts in buckets of equal frequency, or a balanced tree keyed by count) kept current on each change; the trade-off between update cost and query cost.
  • Late events: only buckets newer than now - window - L are open; a late event goes into its own bucket if that bucket still exists.
  • Deduplication: a set of event ids with time-based expiry, sized by rate × (window + L), or a Bloom filter when approximate is fine.
  • Concurrency: shard the counters by URL hash with a lock per shard, or one writer thread fed by a queue; how a query reads a consistent view.
  • Horizontal sharding by URL across machines, each returning its local top-k, and why merging local top-k lists can be inexact; Count-Min Sketch with a heap as the approximate option.

Follow-ups

  • Support any window length per query up to a maximum.
  • Top-k per region or per user segment.
  • Survive a restart without losing the last window of counts.

Where it's asked

  • Amazon · Coding · Last asked Jul 202555 min · Usually two rounds; one is often an object-oriented design built in steps · No AI

What it tests

sliding window · heap · hashing

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding