TRT
Sign in
mediumAdvanceddesign

Design a leaderboard that reports top scores and any player's rank

Combine a hash map with an order-statistic structure to update scores, list the top k and compute ranks in O(log n).

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstChoosing an ordered structurePro — members onlySorted arrays, heaps, balanced trees, skip lists and B-trees all keep data in order, with very different costs. The right choice follows from which operations you need, how often the data changes, and what your language provides.

A strong answer

A strong candidate frames Design a leaderboard that reports top scores and any player's rank around the exact memory layout, compiler semantics, and hardware execution model covered in Choosing an ordered structure. Combine a hash map with an order-statistic structure to update scores, list the top k and compute ranks in O(log n).

Many structures keep data in some kind of order, and they differ in what they can do cheaply. The useful question is never "which structure is best?" but "which operations does this workload need, and how often?". List them first:

The trace solves Contains duplicate III: are there two values at most k positions apart whose difference is at most t? Keep the last k values in an ordered set. For each new value x, a ceiling query finds the smallest stored value at least x − t; if it is also at most x + t, a pair exists. Then add x and remove the value that falls out of the window. Each step is O(log k).

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Match a workload's operations to the cheapest structure that supports them.
  • Mastery of core invariant: Use ordered sets for sliding-window queries, and know the Python, Java and C++ options.
  • Mastery of core invariant: Recognize when a heap, a sorted array or a hash map is enough instead of a tree.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01How do you break ties between equal scores?
  2. 02Why isn't a heap enough?
  3. 03How does Redis implement sorted sets?
  4. 04How would you shard a leaderboard with a hundred million players?

Not started

Sign in to save your learning progress.

Sign in to save