Question

Consider a hash table with 9 slots. The hash function is h(k)=k mod 9h(k) = k \bmod 9. The collisions are resolved by chaining. The following 9 keys are inserted in the order: 5, 28, 19, 15, 20, 33, 12, 17, 10. The maximum, minimum, and average chain lengths in the hash table, respectively, are

Choose one option
Official answer and solution
Official answer
A
From
Official GATE 2014 answer key

Solution

Hash each key into the 9 slots using the modulo hash function and calculate the length of the collision chains.

Hash function: h(k)=k mod 9h(k) = k \bmod 9 on 9 slots (0 to 8).

Compute hash slot for each of the 9 keys in order:

  • h(5)=5 mod 9=5h(5) = 5 \bmod 9 = 5
  • h(28)=28 mod 9=1h(28) = 28 \bmod 9 = 1
  • h(19)=19 mod 9=1h(19) = 19 \bmod 9 = 1
  • h(15)=15 mod 9=6h(15) = 15 \bmod 9 = 6
  • h(20)=20 mod 9=2h(20) = 20 \bmod 9 = 2
  • h(33)=33 mod 9=6h(33) = 33 \bmod 9 = 6
  • h(12)=12 mod 9=3h(12) = 12 \bmod 9 = 3
  • h(17)=17 mod 9=8h(17) = 17 \bmod 9 = 8
  • h(10)=10 mod 9=1h(10) = 10 \bmod 9 = 1

Distribution of keys across the 9 slots:

  • Slot 0: empty (length 0)
  • Slot 1: {28,19,10}\{28, 19, 10\} (length 3)
  • Slot 2: {20}\{20\} (length 1)
  • Slot 3: {12}\{12\} (length 1)
  • Slot 4: empty (length 0)
  • Slot 5: {5}\{5\} (length 1)
  • Slot 6: {15,33}\{15, 33\} (length 2)
  • Slot 7: empty (length 0)
  • Slot 8: {17}\{17\} (length 1)

Analyzing chain lengths:

  • Maximum chain length: max⁡(0,3,1,1,0,1,2,0,1)=3\max(0, 3, 1, 1, 0, 1, 2, 0, 1) = 3.
  • Minimum chain length: min⁡(0,3,1,1,0,1,2,0,1)=0\min(0, 3, 1, 1, 0, 1, 2, 0, 1) = 0.
  • Average chain length: Total keysTotal slots=99=1\frac{\text{Total keys}}{\text{Total slots}} = \frac{9}{9} = 1.

Thus, the maximum, minimum, and average chain lengths are 3, 0, and 1.

Final answer

A

Common trap

Computing average chain length only over non-empty slots (9/6=1.59 / 6 = 1.5) rather than over all 9 slots of the hash table.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2014 Computer Science and Information Technology (CS) Session 1
Question
50 of 65, printed as CS Q.40
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.