Consider a hash table of size seven, with starting index zero, and a hash…

2007

Consider a hash table of size seven, with starting index zero, and a hash function (3x + 4) mod 7. Assuming the hash table is initially empty, which of the following is the contents of the table when the sequence 1, 3, 8, 10 is inserted into the table using closed hashing? Note that ‘_’ denotes an empty location in the table.

  1. A.

    8, _, _, _, _, _, 10

  2. B.

    1, 8, 10, _, _, _, 3

  3. C.

    1, _, _, _, _, _,3

  4. D.

    1, 10, 8, _, _, _, 3

Attempted by 129 students.

Show answer & explanation

Correct answer: B

Hash function: h(x) = (3x + 4) mod 7. Use linear probing for collisions. Table indices are 0 through 6.

  • Insert 1: h(1) = (3*1+4) mod 7 = 7 mod 7 = 0 → place 1 at index 0.

  • Insert 3: h(3) = (3*3+4) mod 7 = 13 mod 7 = 6 → place 3 at index 6.

  • Insert 8: h(8) = (3*8+4) mod 7 = 28 mod 7 = 0 → index 0 is occupied, probe to index 1 which is empty → place 8 at index 1.

  • Insert 10: h(10) = (3*10+4) mod 7 = 34 mod 7 = 6 → index 6 is occupied, probe to index 0 occupied, probe to index 1 occupied, probe to index 2 which is empty → place 10 at index 2.

Final table (indices 0 to 6): 1, 8, 10, _, _, _, 3

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir