skip to content

How do you get the expected number of empty shards when n keys are hashed uniformly across n shards?

level: seniorimportance: should knowfreq 40%

answer

  1. count first, distribution never
  2. one indicator per candidate shard
  3. an indicator's mean is its probability
  4. a key misses a shard with 1 - 1/n
  5. n(1 - 1/n)^n tends to n/e

basics

~20 s

Attach one indicator per shard - worth 1 when that shard stays empty - and add the indicators' probabilities. Each shard is empty with probability (1 - 1/n)^n, so the expected count is n times that, about 0.37n for large n.

solid answer

~40 s

Define one indicator per shard: `X_i = 1` if shard `i` receives no key, else 0. The count you want is `X_1 + ... + X_n`, and its expected value is the sum of the individual expectations, each of which is just a probability. A single key misses shard `i` with probability `1 - 1/n`, so all `n` keys miss it with probability `(1 - 1/n)^n`, which approaches `1/e` as `n` grows. The expected number of empty shards is therefore about `0.368 * n`, leaving roughly 63% of shards occupied. The power of the technique is that it never touches the joint distribution: the shards' occupancies are clearly related - a key landing on one shard is a key not landing on another - and the sum is still correct.

go deeper

for a junior

Recall that an indicator variable is worth 1 when a condition holds and 0 otherwise, and that its average value is simply the probability of that condition. That single fact is what the whole technique rests on.

for a middle

Explain the mechanics: write the count as a sum of one indicator per shard, compute the single probability (1 - 1/n)^n, and multiply by n. Be able to say why the answer is near 37% rather than zero.

for a senior

Show where the average misleads in production. An expected count sizes aggregate cost, but the busiest shard is not an average and needs a separate tail argument; say which of the two a given capacity decision actually depends on.

for a principal

Judge how much modelling the decision deserves. An indicator sum is cheap and often enough to reject a design; commit to a heavier analysis only when the cost of being wrong about the extreme, not the mean, is what drives the spend.

## The technique: turn a count into a sum of indicators An **indicator variable** takes the value 1 when some condition holds and 0 otherwise. The trick that makes it useful is that its expected value *is* the probability of the condition, because `E[X] = 1 * P(condition) + 0 * P(not condition) = P(condition)`. So whenever the quantity you want is a **count of things that happened**, you can write it as a sum of indicators, one per candidate, and then read its expectation off as a sum of ordinary probabilities. The hard part of the original problem - how the candidates interact - is never entered. For `n` keys hashed uniformly into `n` shards: 1. Let `X_i = 1` when shard `i` receives no key at all. 2. The number of empty shards is `X_1 + X_2 + ... + X_n`. 3. `E[X_i] = P(shard i is empty)`, one probability about one shard. 4. The expected number of empty shards is the sum of those `n` probabilities. ## Computing the one probability A single key lands on shard `i` with probability `1/n`, so it misses with probability `1 - 1/n`. The `n` keys are hashed independently of one another, so all of them miss shard `i` with probability `(1 - 1/n)^n`. That expression is the classic approach to `1/e`: | n | (1 - 1/n)^n | Expected empty shards | |---|---|---| | 10 | 0.349 | 3.5 | | 100 | 0.366 | 36.6 | | 1,000 | 0.368 | 367.7 | | limit | 1/e = 0.3679 | 0.368n | So hashing a thousand keys into a thousand shards leaves roughly 368 shards untouched, and only about 632 shards occupied. Engineers who expect a near-perfect spread find this surprising, and it is exactly the sort of figure that decides whether a per-shard fixed cost is affordable. ## Why the sum is legitimate here The shard occupancies are plainly **not** independent: if you learn that shard 1 is empty, the `n` keys must be elsewhere, which makes every other shard slightly more likely to be occupied. Summing expectations is still exactly right. That is the whole appeal of the method - it needs each individual probability and nothing about how they relate, so a problem whose joint distribution is intractable collapses into `n` one-line calculations. The general form is worth carrying: for `m` keys into `n` shards, `P(a given shard is empty) = (1 - 1/n)^m`, roughly `e^(-m/n)`, giving `n * e^(-m/n)` empty shards. At `m = 2n` that is about `0.135n` - doubling the keys does not halve the empties, it cuts them by a factor of e. ## What the method does not give you This is the boundary an interviewer usually probes next. - **It gives an average, not a guarantee.** The expected count says nothing on its own about how far a particular run may sit from it. - **It does not give extremes.** The size of the *busiest* shard is not a sum of indicators, so no amount of linearity produces it. With `n` keys in `n` shards the average load is 1, while the maximum load is far larger - it grows roughly like `log n / log log n` - and a capacity plan built on the average will under-provision the hot shard badly. - **It does not survive a non-uniform hash.** Every term assumed `1/n` per key. If the key distribution concentrates, the per-shard probabilities differ and must be summed individually, which the method still supports, but the tidy `(1 - 1/n)^m` closed form does not. ## Where this shows up in practice - **Expected distinct shards touched** by a batch of lookups, which decides how many connections or round trips a fan-out costs. - **Expected number of occupied buckets** in a hashed structure, which drives memory sizing and decides whether a sparse layout is worth it. - **Expected probe count** in a lookup that retries on a hit, where each probe contributes one indicator. In every case the recipe is identical: name the candidate set, write the condition that makes one candidate count, compute that single probability, and multiply or sum. Reaching for the joint distribution first is the mistake the question is designed to catch.

  • What changes when m keys are hashed into n shards and m is not equal to n?
    Only the one probability. A given shard is missed by every key with probability `(1 - 1/n)^m`, roughly `e^(-m/n)`, so the expected number of empty shards is `n * e^(-m/n)`. At `m = n` that recovers `n/e`; at `m = 2n` it is about `0.135n`, so doubling the keys cuts the empty shards by a factor of e rather than halving them.
  • Does the same indicator sum give the expected size of the busiest shard?
    No. A maximum is not a sum of indicators, so the technique simply does not apply to it. With `n` keys in `n` shards the average load is 1 while the busiest shard holds roughly `log n / log log n` keys. Sizing capacity from the average is how a hot shard gets under-provisioned; the maximum needs a tail bound instead.
  • How would you get the expected number of distinct shards a batch of lookups touches?
    Flip the condition. Use `Y_i = 1` when shard `i` is touched, whose probability is `1 - (1 - 1/n)^m`, and sum over the shards to get `n * (1 - (1 - 1/n)^m)`. For `m = n` that is about `0.632n`, which is the natural complement of the empty-shard count.

saying these in an interview costs you the question

  • Says you must first work out the joint occupancy distribution
  • Claims the indicator sum needs the shards to be independent
  • Answers zero empties because keys and shards are equal in number
  • Confuses the expected empty count with one shard's chance
  • Plans capacity for the busiest shard using the average load