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.

Agree on the scope

Ask what changes the design, then commit to a short list.

Scope

Clarify, then choose

Clarifying requirements

  1. Interviewer

    Design a trending topics system for a social network. What would you ask?

  2. You

    First, what does trending mean?

    • Most mentions: the same names every day.
    • Rising: far above its own usual level. I would choose this, and it needs a history for comparison.
    • Window: one hour, a day and a week. The hour matters most.
  3. Interviewer

    Rising. Scale?

  4. You

    I will assume:

    • 500 million posts a day, about eight topic events each: hashtags, names, links, phrases.
    • That is 4 billion events a day, 46,300 a second, 139,000 at a 3x peak.
    • 55 million different topics a day, 99% of them mentioned fewer than 20 times in an hour.
    • 200 places, three windows, lists of 50.
  5. Interviewer

    How fresh?

  6. You

    A mention should be able to change the hourly list within 60 seconds. The daily and weekly lists may be 10 to 30 minutes old. Reads answer in 50 ms at p99. Two readers may see number eight and nine swapped for a moment.

  7. Interviewer

    People will try to game it.

  8. You

    Then I count each account once per topic per hour, weigh accounts by trust, look for groups that act together, and let people review sensitive topics before they are promoted.

Features

  • Accept topic events
  • Top K for a place and a window
  • Rising score against a baseline
  • Filter by category
  • History of one topic
  • Protection and review

Out of scope

  • Finding phrases in text
  • Personal trends for each reader
  • Ranking by watch time or percentiles
  • Paid placement
  • The posts shown behind a topic

Quality targets

QualityTargetWhy
Accept an event batchp99 under 100 msThe caller should not wait for counting
Read a listp99 under 50 msShown on a home page
Freshness, hourunder 60 sBreaking news
Freshness, day and week10 to 30 minutesThey move slowly
Reads available99.9%, old lists allowedA list that is five minutes old is fine
Events durableNot lost once acceptedLists can be rebuilt
AccuracyTop 10 right, counts within 1%People see order, not counts

Each answer changes the design

You askIf the answer isThen
Known items only?Yes, 10,000 songsA table of counters, no sketch
Exact counts for money?YesDifferent system: see ad clicks
Any window, such as 37 minutes?YesKeep minute buckets for longer, or query an OLAP store
Daily list only?YesA nightly job
Posts or people?PostsSimpler, and easier to fake