Skip to content
Official PYQ · 2007

GATE CS 2007 Q40 — Arrays, Stacks and Queues

Explain with AI

Consider a hash table of size seven, with starting index zero, and a hash function (3x+4)mod  7(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,101, 3, 8, 10 is inserted into the table using closed hashing? Note that − denotes an empty location in the table.

Choose one option
View worked solution

Answer: B

We are given a hash table of size N=7N = 7 (with indices 00 to 66) and the hash function:

h(x)=(3x+4) mod 7h(x) = (3x + 4) \bmod 7

Since "closed hashing" (also known as open addressing) is used, we resolve collisions using linear probing, where we search sequentially for the next available slot: hi(x)=(h(x)+i) mod 7h_i(x) = (h(x) + i) \bmod 7 for i=0,1,2,…i = 0, 1, 2, \dots.

Let's insert the keys 1,3,8,101, 3, 8, 10 step-by-step into an initially empty table:

  1. Insert 11:
    h(1)=(3×1+4) mod 7=7 mod 7=0h(1) = (3 \times 1 + 4) \bmod 7 = 7 \bmod 7 = 0.
    Slot 00 is empty. Place 11 at index 00.
    Table state: [1, -, -, -, -, -, -]

  2. Insert 33:
    h(3)=(3×3+4) mod 7=13 mod 7=6h(3) = (3 \times 3 + 4) \bmod 7 = 13 \bmod 7 = 6.
    Slot 66 is empty. Place 33 at index 66.
    Table state: [1, -, -, -, -, -, 3]

  3. Insert 88:
    h(8)=(3×8+4) mod 7=28 mod 7=0h(8) = (3 \times 8 + 4) \bmod 7 = 28 \bmod 7 = 0.
    Slot 00 is occupied by 11 (collision).
    Using linear probing, we try the next slot: (0+1) mod 7=1(0 + 1) \bmod 7 = 1.
    Slot 11 is empty. Place 88 at index 11.
    Table state: [1, 8, -, -, -, -, 3]

  4. Insert 1010:
    h(10)=(3×10+4) mod 7=34 mod 7=6h(10) = (3 \times 10 + 4) \bmod 7 = 34 \bmod 7 = 6.
    Slot 66 is occupied by 33 (collision).
    Probing sequence:
    • Try slot (6+1) mod 7=0(6 + 1) \bmod 7 = 0 (occupied by 11)
    • Try slot (0+1) mod 7=1(0 + 1) \bmod 7 = 1 (occupied by 88)
    • Try slot (1+1) mod 7=2(1 + 1) \bmod 7 = 2 (empty)
    Place 1010 at index 22.
    Table state: [1, 8, 10, -, -, -, 3]

Thus, the final contents of the hash table are 1,8,101, 8, 10, −, −, −, 33, which matches option B.

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

Published
Report a correction
Keep exploringMore Arrays, Stacks and Queues questions