← Questions

Real-time top-k products from purchase events

0
AmazonCodingLast asked Sep 2025

A coding interview question asked in Amazon's coding round, last asked Sep 2025. It tests heap, hashing and streaming.

Product events arrive as a continuous stream, each (timestamp, product_id, event_type) with event_type one of "view", "add_to_cart" or "purchase". Each type has a configurable weight, and a product's score is the sum of the weights of its events. Build a class that ingests events and returns the k highest-scoring products at any moment, over all time and, optionally, over only the last W seconds.

Interface

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

  • TopKProducts(weights: dict[str, int], window: int | None = None) — for example {"view": 1, "add_to_cart": 3, "purchase": 5}.
  • update(timestamp: int, product_id: str, event_type: str) -> None
  • top_k(k: int, now: int | None = None) -> list[str] — highest score first; ties broken by product id, ascending. With a window, only events in (now - W, now] count.

Example

t = TopKProducts({"view": 1, "add_to_cart": 3, "purchase": 5}, window=60)
t.update(0,  "p1", "view")
t.update(10, "p2", "purchase")
t.update(20, "p1", "add_to_cart")
t.update(30, "p3", "view")
t.top_k(2, now=30)    ->  ["p2", "p1"]     # p2 5, p1 4, p3 1
t.update(65, "p3", "add_to_cart")
t.top_k(2, now=65)    ->  ["p2", "p3"]     # p1's view at 0 expired: p2 5, p3 4, p1 3

Constraints

  • Millions of events; many products; k small (up to about 100).
  • Events can arrive slightly out of order.

Clarifying questions

  • Which matters more, update speed or query speed?
  • How exact must the answer be? Is an approximate top k acceptable at very high volume?
  • How late can an event arrive, and should late events still count?
  • Will several threads call update at once?

What a strong answer covers

  • Exact version: a hash map from product to score plus an ordered structure keyed by (-score, product_id) (a balanced tree or sorted list), so each update is O(log P) and top_k is O(k). A heap with lazy deletion is the alternative; say what each costs.
  • Sliding window: a queue of events (or per-second buckets) in time order; expired events subtract their weight when the window moves. Store the weight applied with each event so a later weight change does not corrupt the totals.
  • Memory: bounded by the products with events in the window; at very high volume, a Count-Min sketch or Space-Saving summary with a bounded heap, and their error.
  • Out-of-order and replayed events: accept within a grace period, ignore older ones, and make replay idempotent with event ids.
  • Concurrency: a lock per shard of products, or a single writer fed by a queue.

Follow-ups

  • Make it work across many machines.
  • Scores should decay over time instead of dropping off a window edge.
  • Report the top k per category.

Where it's asked

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

What it tests

heap · hashing · streaming

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