A hash function f defined as f(key) = key mod 7, with linear probing used to…

2009

A hash function f defined as f(key) = key mod 7, with linear probing used to resolve collisions. Insert the keys 37, 38, 72, 48, 98 and 11 into the table indexed from 0 to 6. What will be the location of 11 ?

  1. A.

    3

  2. B.

    4

  3. C.

    5

  4. D.

    6

Show answer & explanation

Correct answer: C

In open addressing with linear probing, a key's home slot is given by h(key) = key mod m, where m is the table size. If that slot already holds another key, the search moves forward one slot at a time, h(key)+1, h(key)+2, and so on (wrapping past the last index back to 0), stopping at the first empty slot found.

  1. Insert 37: h(37) = 37 mod 7 = 2. Slot 2 is empty, so 37 is placed at slot 2.

  2. Insert 38: h(38) = 38 mod 7 = 3. Slot 3 is empty, so 38 is placed at slot 3.

  3. Insert 72: h(72) = 72 mod 7 = 2. Slot 2 already holds 37, so probe slot 3, which already holds 38, so probe slot 4, which is empty; 72 is placed at slot 4.

  4. Insert 48: h(48) = 48 mod 7 = 6. Slot 6 is empty, so 48 is placed at slot 6.

  5. Insert 98: h(98) = 98 mod 7 = 0. Slot 0 is empty, so 98 is placed at slot 0.

  6. Insert 11: h(11) = 11 mod 7 = 4. Slot 4 already holds 72, so probe the next slot, slot 5, which is empty; 11 is placed at slot 5.

Recount the final table: slot 0 holds 98, slot 2 holds 37, slot 3 holds 38, slot 4 holds 72, slot 5 holds 11, and slot 6 holds 48 - six keys in six distinct slots out of seven, a load factor of 6/7, with no slot reused. Independently, 11 = 7x1 + 4 confirms the home slot is 4; since slot 4 is already occupied by 72, the next slot in the probe order, slot 5, is where 11 comes to rest.

So the key 11 is stored at index 5.

Explore the full course: Coding For Placement

Loading lesson…