GATE Computer ScienceHardGATE CS (PYQ pattern)

Keys 19, 36, 5, 21, 4, 26, 14, 17, 25, 12, 40, 33 are inserted into a chained hash table with 7 buckets, h(k) = k mod 7, each new key appended at the end of its chain. Counting one comparison per key examined, what is the average cost of a successful search (each stored key equally likely) and of an unsuccessful search (each bucket equally likely, the whole chain scanned)?

  1. A. 29/12 and 19/7
  2. B. 29/12 and 12/7
  3. C. 17/12 and 12/7
  4. D. 29/7 and 12/7

Correct answer: B. 29/12 and 12/7

Chains: 0: 21, 14; 1: 36; 3: 17; 4: 4, 25; 5: 19, 5, 26, 12, 40, 33; buckets 2 and 6 are empty. Successful: a key at position p costs p, so the total is 3 + 1 + 1 + 3 + 21 = 29 over 12 keys. Unsuccessful: scanning a whole chain costs its length; the lengths add up to 12 over 7 buckets, 12/7. Counting a final NULL check as an extra comparison would give 19/7.

More Programming and Data Structures (GATE) questions

Exams that ask this topic