Skip to content
Official PYQ · 2022

GATE CS 2022 Q6 — Searching, Sorting, Hashing

Explain with AI

​​​Suppose we are given nn keys, mm hash table slots, and two simple uniform hash functions h1h_{1} and h2.h_{2}. Further suppose our hashing scheme uses h1h_{1} for the odd keys and  h2h_{2} for the even keys. What is the expected number of keys in a slot?

Choose one option
View worked solution

Answer: B

Under the simple uniform hashing assumption, each key is equally likely to hash to any of the mm slots, independent of where other keys have hashed.

Let the total number of keys nn be divided into:

  • non_o odd keys, which are hashed using h1h_1
  • nen_e even keys, which are hashed using h2h_2

where no+ne=nn_o + n_e = n.

Let XjX_j be the random variable representing the total number of keys in a specific slot jj (where 1≤j≤m1 \le j \le m). We can express XjX_j as the sum of two components:

Xj=Xo,j+Xe,jX_j = X_{o, j} + X_{e, j}

where:

  • Xo,jX_{o, j} is the number of odd keys hashing to slot jj.
  • Xe,jX_{e, j} is the number of even keys hashing to slot jj.

By the definition of simple uniform hashing:

  • Each of the non_o odd keys has a probability of 1m\frac{1}{m} of hashing to slot jj. Thus, the expected number of odd keys in slot jj is: E[Xo,j]=nomE[X_{o, j}] = \frac{n_o}{m}
  • Each of the nen_e even keys has a probability of 1m\frac{1}{m} of hashing to slot jj. Thus, the expected number of even keys in slot jj is: E[Xe,j]=nemE[X_{e, j}] = \frac{n_e}{m}

Using the linearity of expectation, the expected total number of keys in slot jj is:

E[Xj]=E[Xo,j]+E[Xe,j]=nom+nem=no+nem=nmE[X_j] = E[X_{o, j}] + E[X_{e, j}] = \frac{n_o}{m} + \frac{n_e}{m} = \frac{n_o + n_e}{m} = \frac{n}{m}

Thus, the expected number of keys in any slot is nm\frac{n}{m}.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Searching, Sorting, Hashing questions