First, the number of possible combinations is not n^2
but n^n
since each of the n
keys has n
possibilities to land in a slot.
Next, due to all slots being symmetric, the expected number of empty slots E = n * P
, where P
is the probability that each single slot ends up empty. This is due to linearity of expectation which holds even when the random values are dependent.
Now, note that the probability Q
that a single key does not land in this slot is Q = (n - 1) / n
.
Since there are n
keys, the probability P
that no key lands in a fixed slot is Q ^ n
.
Summing all that up, we have E = n * ((n - 1) / n)^n
. The limit of (n - 1/n)^n
is 1/e
(see here), thus the expected number of empty slots is n / e
.