Design an E-commerce Best-Sellers Ranking (Top-K Products)
The prompt arrives in two parts and sounds like one feature: "You're working on ClipKart.com, a large e-commerce platform. We want a button: click it and you see the Top 10 most-bought products in the last N hours, where N is 1, 4 or 24. And we want best-seller ranks: one product might be the #1,056th best-seller overall but #13 in Sports Equipment and #24 in Safety. Design the system."
Most candidates hear "Top-K" and reach for the textbook: a min-heap, maybe a Count-Min Sketch, a box labelled "stream processor". That answers the first half and can't answer the second at all. A heap of ten tells you who is #1. It can't tell a helmet seller that their product is #1,056 overall, because answering that means knowing the order of the 1,055 products above it. These are two different problems in one costume. The button needs only the head of a list, exact and fresh to the minute, over windows that slide. The rank badge needs the entire order of all 20,000 categories, and it can safely be an hour old.
The arithmetic makes the split obvious. ClipKart sells to 100 million shoppers a day. That's 60 million order lines, about 700 a second on average and 15,000 a second at the peak of a sale. That volume is small, and the streaming state that counts it exactly fits in about 5.6 GB. The hard part is the shape of the questions. You need exact sliding windows that also take back cancelled orders. You need a merge across 64 partitions that is provably correct. You need a full sort of 240 million category memberships every hour. And you need to serve those ranks to 116,000 product-page views a second without ever showing two products as "#13 in Sports Equipment".
The Trap Built Into a Famous Question
Two failures show up again and again:
- The Heap Reciter: draws Count-Min Sketch plus a min-heap for everything. The Top-10 button works, approximately. The "#1,056 overall" badge can't exist, and a seller who disputes losing the #1 badge to a sketch's overcount is owed an answer the design doesn't have. Signal: recited a pattern instead of reading the question.
- The Live Sorter: keeps one sorted set per category and increments it on every purchase. Ranks are live, and they flicker. "Last 1 hour" now needs a retraction for every expiring sale, and decay means rescoring every member. Meanwhile the 40-million-member "overall" set can't be split, so one shard carries every write with no room to grow. Signal: built one mechanism for two problems and paid for it twice.
The passing performance separates the two questions in the first five minutes. It then builds a streaming speed path that keeps exact per-minute counts, partitioned by product so the merge is provably exact, and an hourly rank path that sorts every category and publishes ranks as a versioned snapshot flipped atomically. Because a #1 badge is worth money, it also defends both paths against sellers who try to buy their way to the top.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson