Suppose our hash table has slots. Under the simple uniform hashing assumption, what is the expected number of distinct elements that needs to be inserted into the table to get the first hash collision, i.e, a slot with two elements?
This problem is a part of the Hash Crash Series
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
A great problem, @Agnishom Chattopadhyay .
The probability that an element collides after n elements are inserted (means there are total of n + 1 elements) is
P ( n ) = 1 0 0 0 n 1 0 0 0 × ( 1 0 0 0 − 1 ) × ⋯ × ( 1 0 0 0 − n + 1 ) × 1 0 0 0 n .
Hence, the expected value is
n = 0 ∑ 9 9 9 ( n + 1 ) P ( n ) = 4 0 . 3 0 3 2 1 2 9 2 6 2
Here is the code :
If this is not convincing enough, let's try Monte-Carlo simulation
Which returns
40.316
.