AlgoViz

Design a Top K / Trending Topics System

hard 45 min

Count billions of mentions a day and show the ten topics that are rising right now, in each country and city.

How to approach it

What this question tests, where it gets hard, and how to spend the time.

Read the question

What this problem tests

  • Can you define the answer?

    Popular or rising, posts or people, which window, which place. The formula decides the design.

  • Can you split a ranking?

    Merging partial lists is wrong unless the keys are divided in the right way.

  • Can you bound memory?

    Say the error you accept, size the structure from it, and say when exact would do.

  • Do you expect opponents?

    A trends list is worth faking. Inputs are not to be trusted.

Think

Five questions before you draw

Ask yourselfHere
Who does what?Millions write posts. Millions read one short list
What must never go wrong?A harmful or faked topic at number one
What is the hardest thing?The true top K when counts are spread and unbounded
What breaks first as it grows?Writes to one table, then memory, then one hot topic
What am I giving up?Exact counts for rare topics, and up to a minute of freshness

The order of the conversation

Spend the 45 minutes

MinutesDoLeave withSkip
0 to 5Define trending, windows, placesFreshness targetPersonal trends
5 to 10Events, distinct topics, listsWhat must be splitNetwork cards
10 to 15Read and write interfaceEvent and list shapeAuth details
15 to 25Grow from one tableTwo-stage countingProduct names
25 to 40Merging, memory, score, abuseTwo decisions with numbersProofs
40 to 45Failures and recapA clear last sentenceNew features

Lines you can open with

  1. 1

    Start

    "Before I design it, I would like to agree what trending means, because it decides what I must remember."

  2. 2

    Commit

    "I will count accounts, not posts, over the last hour, and compare with the same hour on earlier days."

  3. 3

    When stuck

    "Let me follow one mention from the post to the list and ask where it is counted."

  4. 4

    Close

    "Each topic has one counter, each counter names its best, and readers only ever see finished lists."

Do

  • Say why partition key matters for correctness
  • Size the sketch from the error
  • Offer the exact version and its cost
  • Give the freshness budget

Traps

  • "Use Redis sorted sets" for 46,300 increments a second and every region
  • Listing the contents of a sketch
  • Merging top lists of random partitions
  • Ignoring fake accounts