GOAT EngineerPreparing your learning space…

System Design · Practice problem · Medium

Build a Trending Top K

Count each event into short time buckets with a stream processor, drop each bucket as it leaves the window, and write the ranked result to one fast record. Readers fetch that record instead of recounting every item.

Medium means two needs that pull against each other. You solve it on a canvas that runs your design. The canvas needs JavaScript; everything on this page works without it.

What is the situation?

Your sports app has a strip on its home screen called Hot Right Now: the ten most played clips of the last 30 seconds. During a match, 600 plays a second arrive across 5,000 clips, with a few clips taking most of them, and 300 people a second load the strip. A goal can send one new clip to 1,200 plays a second within a moment, and clips fade just as quickly, so a clip nobody has played for 30 seconds should be off the list. Design POST /plays and GET /hot so both stay quick and the list stays close to the truth. How close is measured like this. Take the ten clips your design would show, add up how many times each one was really played in the last 30 seconds, and divide by the same sum for the true top ten. One clip swapped with its near neighbour barely moves that figure, and a viral clip that is missing moves it a lot. It reads the list your design last wrote, and the bar is its worst second. Three facts about this simulator decide what you can build. A count kept in a Database, Counter Store or Cache is a running total since the run began, and it cannot be kept per second or made to expire. A query that adds up every clip's count takes 10 microseconds a clip on a Database and 1 on a Counter Store, all in one slot on one server, and adding servers does not make that sum cheaper here. And the oldest copy a reader can be handed is set by the lifetime of any Cache or CDN on the way.

What must your design do?

  • POST /plays and GET /hot each answer within 50 ms at the 99th percentile.
  • At least 99.9% of plays are accepted, and at least 99.9% of list loads are served.
  • At its worst second, the list holds at least 85% of the plays the true top ten had on a normal stretch, and at least 70% while clips are taking off or a stream worker is down.
  • No reader can be handed a copy of the list older than 10 seconds.
  • The system costs $650 a month or less, with each test billed as if its traffic ran all month.

What does Run test?

Run works without an account. It checks your design against 2 tests you can read before you start:

  • A normal stretch of the match. 600 plays and 300 list loads a second for 55 seconds. We judge from second 35, once 30 full seconds of plays are behind the list.
  • A goal. At second 40, a goal sent one new clip to 1,200 plays a second for 15 seconds, on top of the usual traffic.

What does Submit add?

Submit needs an account. It runs 3 more tests you cannot read in advance, and stops at the first one your design fails, showing what happened and why. Passing every test, or giving up, unlocks the written solution.

Which parts can you use?

  • Load Balancer: about 50,003 a second, $18 a month each
  • App Server: about 2,000 a second, $45 a month each
  • Queue: effectively unlimited, $25 a month each
  • Stream Processor: about 50,078 a second, $90 a month each
  • Cache: about 100,003 a second, $30 a month each
  • Database: about 5,000 a second, $120 a month each
  • Counter Store: about 100,003 a second, $30 a month each

Every connection takes 1 ms, and the budget is $650 a month.

What does this problem leave out?

  • Approximate counters such as a Count-Min Sketch: every count here is exact, and the memory a window holds is not priced.
  • A merge that loses a clip: each clip lives on one lane here, so merging the lanes' lists loses nothing. With a clip counted on several tasks, a real merge can miss it.
  • Plays that arrive very late: a play is counted under the second it happened, whatever kept it waiting.
  • Bots and fake plays, and separate lists per sport or region.
  • A Database, Counter Store or Cache losing what it holds: no test here wipes one.

Where does this problem fit?

Its category is Analytics & leaderboards: Aggregate continuously. It practises caching, partitioning, stream processing and reliability & ops.

For the whole system, read the interview breakdown Design a Top K System (Spotify, YouTube).

See every practice problem.

By GOAT Engineer. Explore System Design · Privacy Policy · Contact.