The challenge says that there is a hash algorithm with a bug, the bug is given if it reaches the end of the array and did not find space to save the element, it discards it directly (that is, it does not try to place it in positions 0, 1, 2 ... as it would be correct)
In a hash table using closed hash, linear probing, probe length 1.
What is the probability that with this test we discover that the implementation n has a bug, if N = 27 and K = 42?
Example K=2, N=2: The only case is if both hashes are 1. In this case, the first element in array [ 1 ] is first saved. Then, the next element does not have space in array [ 1 ] and since it is the last position of the array, will discard it
I made a script to bruteforce all combinations and find the probability for the firsts N / K
The pseudocode is:
function bruteforce(array):
hashtable=
{0="" , 1="" , ..., ..., k-1=""}
for element in array:
canStoreThisCombination=False
for j in range (element,len(hashtable)):
if (empty(hashtable[j])):
hashtable[j]="used"
canStoreThisCombination=True
break
if not canStoreThisCombination:
tot=tot+1
for each combination of N elements:
bruteforce(actualCombination)
print tot/(k^n)
and got the following results:
I'm looking for probability as an irreducible fraction