5 Primitive hash table in action

Let us define MAX_ENTRIES to 256. Now, let’s see what happens when we insert values into the hash table. Let us first insert the key-value pair ("Tak Auyeung", "Earth"). The the sum of the ASCII code of Tak Auyeung, which adds up to 1054, but the hash value is the mod 256 result, which is 30.

Now, let’s try to add another key-pair entry ("Kat Auyeung", "Mars"). Hey, the ASCII codes add up to 1054, and the hash value is 30!

If we used the logic in the previous section, the second insertion would have overwritten the entry from the first insert. However, size is incremented correctly. Worse, if we look up Tak Auyeung, the value is Mars instead of Earth. Yikes!

This problem is due to both "Tak Auyeung" and "Kat Auyeung" map to the same hash value. This is not difficult to understand, as Kat is Tak spelt backward. The upper case is not important because both names only have one upper case letter and two lower case letters (and addition is commutative).

How should we fix this problem?