Lesson 2 of 15
35. Design a Leaderboard (Top-K)

35.1 What do you ask first?

3 min read+10 XP

Step 1: Requirements (5 minutes)

LetterCandidate asksInterviewer answers
Scope"How are points calculated? One contest or many? Do we show top-N, my rank, friends' ranks? Prizes at the end?""Players earn points per event; captain counts 2×, vice-captain 1.5×. Many contests, but one mega contest dominates. Show top 100 and my rank. Final ranks decide prizes."
Count"How many users in the biggest contest?""20 million teams."
Access pattern"How often do users check? How often do points change?""They refresh constantly during the match. Points change almost every ball."
Latency"How fresh must ranks be? How fast must the page load?""A few seconds to half a minute behind the match is fine. The page must load fast."
Exactness"Must live ranks be exact? Final ranks?""Live ranks can lag slightly. Final ranks must be exact; money depends on them."

Agreed requirements:

  • Functional (verbs): compute team scores from player events; show the top 100; show a user's rank and score; finalise exact ranks for prizes; handle ties fairly.
  • Non-functional (adjectives): fast reads (my rank, top 100) under heavy refresh traffic; fresh within ~10-30 s; scalable to 20 million teams with huge update bursts; exact and auditable at the end; available during the match.

Interviewer pushback:

  • "Why does the update pattern matter so much?" → Because it's unusual: one real event (a six by a popular player) changes the score of every team that picked him, which can be most of the contest. That's millions of writes from one ball, not one write per user action.

Memory code: "Ask what changes a score, and how many scores one event changes."