Skip to content
Exercises · Q8

Q.Use the hash function: h(element)= element%11 to store the collection of numbers: [44, 121, 55, 33, 110, 77, 22, 66] in a hash table. Display the hash table created. Search if the values 11, 44, 88 and 121 are present in the hash table, and display the search results.

Uttar Pradesh UpmspTextbookSubjective· 3mImportance★★★★★
88% · 15/17 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

A hash table uses a hash function to map keys to indices; here h(element) = element % 11 maps each number to a slot, and collisions are resolved by linear probing (checking the next available slot). We build the table, then search for each target value.

This is a hash table construction and search problem. The core idea is that a hash table gives us near-constant-time lookups by using a function to compute where an element should be stored. When two elements map to the same slot (a collision), we need a strategy — here, the natural choice is linear probing: if a slot is occupied, try the next one (wrapping around if needed).

We have 11 slots (indices 0 through 10) because the hash function uses modulo 11. The numbers to insert are: 44, 121, 55, 33, 110, 77, 22, 66.

Let's build the table step by step.

Insert 44: 44 % 11 = 0. Slot 0 is empty → store 44 at index 0.

Insert 121: 121 % 11 = 0. Slot 0 is occupied. Linear probe: check slot 1 → empty → store 121 at index 1.

Insert 55: 55 % 11 = 0. Slot 0 occupied, slot 1 occupied, slot 2 empty → store 55 at index 2.

Insert 33: 33 % 11 = 0. Slots 0,1,2 occupied, slot 3 empty → store 33 at index 3.

Insert 110: 110 % 11 = 0. Slots 0,1,2,3 occupied, slot 4 empty → store 110 at index 4.

Insert 77: 77 % 11 = 0. Slots 0–4 occupied, slot 5 empty → store 77 at index 5.

Insert 22: 22 % 11 = 0. Slots 0–5 occupied, slot 6 empty → store 22 at index 6.

Insert 66: 66 % 11 = 0. Slots 0–6 occupied, slot 7 empty → store 66 at index 7.

The final hash table (indices 0 to 10) is:

IndexValue
044
1121
255
333
4110
577
622
766
8—
9—
10—

Now, searching. For each target, we compute its hash index, then probe linearly until we either find it or hit an empty slot (meaning it's absent).

Search for 11: 11 % 11 = 0. Check slot 0: 44 ≠ 11. Slot 1: 121 ≠ 11. Slot 2: 55 ≠ 11. Slot 3: 33 ≠ 11. Slot 4: 110 ≠ 11. Slot 5: 77 ≠ 11. Slot 6: 22 ≠ 11. Slot 7: 66 ≠ 11. Slot 8: empty → stop. Not found.

Search for 44: 44 % 11 = 0. Slot 0: 44 == 44 → Found at index 0.

Search for 88: 88 % 11 = 0. Check slot 0: 44 ≠ 88. Slot 1: 121 ≠ 88. Slot 2: 55 ≠ 88. Slot 3: 33 ≠ 88. Slot 4: 110 ≠ 88. Slot 5: 77 ≠ 88. Slot 6: 22 ≠ 88. Slot 7: 66 ≠ 88. Slot 8: empty → stop. Not found.

Search for 121: 121 % 11 = 0. Slot 0: 44 ≠ 121. Slot 1: 121 == 121 → Found at index 1.

Watch out

A common mistake is to stop searching as soon as you see a value that doesn't match, without continuing to probe. Because of collisions, the target might be further down the probe sequence. Always probe until you either find the value or hit an empty slot.

Here's the Python code that implements this:

class HashTable:
    def __init__(self, size=11):
        self.size = size
        self.table = [None] * size

    def hash_function(self, key):
        return key % self.size

    def insert(self, key):
        index = self.hash_function(key)
        # Linear probing: find the next empty slot
        while self.table[index] is not None:
            index = (index + 1) % self.size
        self.table[index] = key

    def search(self, key): …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.