Hashing & collision handling

Computer Science · Algorithms

Question 1 of 10

0

    A hash table has 6 slots numbered 0 to 5. The hash function is h(k) = k mod 6. Which slot number does the key 267 hash to?

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Hashing & collision handling explained

    A hash table stores items so that they can be found quickly, by using a hash function to turn each key into a position in an array. A simple hash function divides the key by the size of the table and uses the remainder as the position. Because many keys can share the same remainder, two different keys may be given the same position. This is called a collision, and a good hash function spreads keys evenly so that collisions are rare.

    There are two common ways to handle collisions. Chaining stores every item that hashes to the same position in a linked list held at that position, so the table can always accept more items. Linear probing places the new item in the next free position after the one it hashed to, checking each position in turn and wrapping back to the start of the table if needed.

    Linear probing keeps all items in the array itself, but clusters of filled positions can form and slow down later searches. A hash table works best when it is not too full, so programmers often make the table larger when it reaches a certain fill level.

    Worked example

    A hash table has 7 positions and uses the hash function h(k) = k mod 7 with linear probing. Insert the keys 15, 22 and 8 in that order.

    1. 15 mod 7 = 1, so 15 goes into position 1.
    2. 22 mod 7 = 1, which collides with 15. Position 2 is free, so 22 goes into position 2.
    3. 8 mod 7 = 1 collides, then positions 1 and 2 are full, so 8 goes into position 3.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      A hash table has 5 slots numbered 0 to 4. The hash function is h(k) = k mod 5. Which slot number does the key 701 hash to?

    • Medium

      A hash table has 8 slots numbered 0 to 7 and uses h(k) = k mod 8. Collisions are resolved by linear probing: if a slot is full, try the next slot, wrapping from the last slot back to slot 0. The keys 33, 74, 41, 96, 43 are inserted in that order into an empty table. In which slot is the last key, 43, stored?

    • Hard

      A hash table has 13 slots numbered 0 to 12 and uses h(k) = k mod 13 with linear probing (next slot, wrapping round). The keys 134, 121, 37, 129, 123, 140, 10, 128 are inserted in that order into an empty table. The key 30 is not in the table. How many slots are examined when searching for it, counting the empty slot that ends the search?