All Exams Test series for 1 year @ ₹349 only
Question

A Hash function $f$ is defined as $f(key) = key \pmod 7$. With linear probing, while inserting the keys $25, 34, 41, 56, 19, 72$ into a table indexed from $0$, in which location the key $72$ will be stored (count table Index $0$ as $0^{th}$ location)?

The correct answer is

2

Hashing and Linear Probing Explained

This solution explains the process of inserting keys into a hash table using a specific hash function and linear probing for collision resolution. We will trace the insertion of the given keys to determine the final storage location for the key 72.

Hash Function and Table Setup

The hash function is defined as $f(key) = key \pmod 7$. This means the hash table will have indices ranging from 0 to 6. Linear probing is used to handle collisions, where we sequentially search for the next available slot.

Step-by-Step Key Insertion

We insert the keys in the given order: 25, 34, 41, 56, 19, 72.

  • Insert 25:
    • Hash value: $25 \pmod 7 = 4$.
    • Index 4 is empty. Store 25 at index 4.
    • Table state: [_, _, _, _, 25, _, _]
  • Insert 34:
    • Hash value: $34 \pmod 7 = 6$.
    • Index 6 is empty. Store 34 at index 6.
    • Table state: [_, _, _, _, 25, _, 34]
  • Insert 41:
    • Hash value: $41 \pmod 7 = 6$.
    • Index 6 is occupied by 34.
    • Probe next location: $(6 + 1) \pmod 7 = 0$.
    • Index 0 is empty. Store 41 at index 0.
    • Table state: [41, _, _, _, 25, _, 34]
  • Insert 56:
    • Hash value: $56 \pmod 7 = 0$.
    • Index 0 is occupied by 41.
    • Probe next location: $(0 + 1) \pmod 7 = 1$.
    • Index 1 is empty. Store 56 at index 1.
    • Table state: [41, 56, _, _, 25, _, 34]
  • Insert 19:
    • Hash value: $19 \pmod 7 = 5$.
    • Index 5 is empty. Store 19 at index 5.
    • Table state: [41, 56, _, _, 25, 19, 34]

Determining Location for Key 72

Now, we insert the key 72 into the table.

  • Insert 72:
    • Hash value: $72 \pmod 7 = 2$.
    • Index 2 is empty.
    • Store 72 at index 2.

Final Storage Location

After performing the hash calculations and applying linear probing for collisions, the key 72 is stored at table index 2.

Was this answer helpful?

Important Questions from Searching, Sorting and Hashing

  1. How many number of comparison are required in worst case to find out second smallest element of n elements list?
Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App