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

Score yourself

3 min read+10 XP

Score yourself

Give yourself 0, 1 or 2 points per row.

Area0 points1 point2 points
RequirementsStarted drawingListed top-N and my rankFan-out of one event, freshness, ties and exact finals agreed
EstimatesNoneNumbers, not used laterNumbers that showed write bursts and read volume drive the design
API and dataSkippedAPI or store, not bothReads with updated_at; teams, player points, sorted sets and finals separated
High-level designORDER BY per requestSorted set, no update planBatched rescoring, cached top-K, per-shard rank, final job
Deep diveHand-wavedSorted set describedUpdate bursts, ties or sharding handled with numbers
Trade-offsNone namedNamed when askedRaised unprompted (freshness vs load, exact vs approximate rank, shard key)
CommunicationLong silencesMostly clearThought out loud, checked in, managed time

12 to 14: strong hire signal. 8 to 11: solid, polish the weak rows. Under 8: redo the round in a week, timer on.

The bar: fresher vs SDE-2

FresherSDE-2
StructureSorted set, ZADD and ZREVRANKKnows costs, top-K merge, ZCOUNT for rank
UpdatesUpdate a score when it changesNotices one event changes millions; batches and indexes player → teams
TiesNot mentionedComposite score, competition ranking, prize rules
ScaleOne RedisSharded by team, cached top-K, approximate percentiles
MoneyUses the live boardSeparate exact, audited final job

Common mistakes

  • SELECT ... ORDER BY points on every refresh.
  • Updating every team on every ball, with no plan for millions of writes per event.
  • Ignoring ties, so equal teams jump around between refreshes.
  • Paying prizes from the live leaderboard.
  • Sharding by score range without discussing hot shards and moving members.

Quick check

Five questions on this chapter. Pick an answer, then check it.

quiz