Study resource
Hash tables study guide
Study Hash tables with curriculum-aligned Study Guide resources, practice links, and exam-focused support.
At a glance
study guide
Resource type
Topic
Hash tables
Study guide overview
Deeper Study Guide: Applying Hash Tables and Rehashing
Use this guide to reason from keys and values through hashing, detect collisions accurately, and explain how rehashing resolves them in exam-style questions.
A method for solving hash-table questions
When given a hash table problem, separate the information into three parts: the key, the hashing algorithm, and the value mapped to the key. First apply the stated hashing algorithm to the key. Next use the resulting hash to identify the relevant table position. Finally check whether another key has already produced that same hash.
For a simple algorithm such as
hash(key) = key mod 5, calculate the remainder after division by5. For key23, the result is3, because23 mod 5 = 3. If key8is also considered,8 mod 5 = 3. These are different key values with the same hash, so they create a collision.Reasoning about a collision
Do not decide that a collision exists merely because two keys look similar or because two values are similar. Perform the hashing calculation for each key and compare the results. A collision exists precisely when the two key values compute the same hash.
A strong explanation can be written as a chain:
- Apply the given hashing algorithm to the first key.
- Apply the same algorithm to the second key.
- Compare the two hash results.
- If the results are equal, state that a collision has occurred.
- Apply the specified rehashing process to handle the collision.
For example, with
hash(key) = key mod 5, keys12and17both produce2. The conclusion is not that12and17are the same key. The conclusion is that both keys initially identify hash position2, so the second placement or lookup requires rehashing according to the rule given in the question.Explaining rehashing precisely
Rehashing is the collision-handling mechanism in this topic. It means carrying out a further hashing calculation when the original calculation leads to a position already associated with another key. In an exam, use the rehashing formula or procedure supplied by the question. Show each calculation and identify the resulting position rather than writing only that the item is moved.
The purpose of rehashing is to preserve the mapping between distinct keys and their values even when the initial hash is identical. A rehashed position is not evidence that the keys have become equal; it is the result of resolving their shared initial hash.
Exam application checklist
Before finalising an answer, check that you have:
- identified which data is the key and which is the value;
- written or used the simple hashing algorithm correctly;
- shown the arithmetic for each relevant key;
- stated the resulting hash or table position;
- identified a collision only when two key values have the same hash;
- described rehashing as the response to that collision;
- used the supplied rehashing rule rather than inventing an unsupported one.
Self-check questions
- What is the difference between a key, a value and a hash?
- If two different keys produce the same hash, what is this called?
- Why is comparing the original keys important before claiming that a collision exists?
- What calculation is performed after an initial position is unavailable because of a collision?
- Given
hash(key) = key mod 5, calculate the hashes for11and16, then explain whether a collision occurs.
For the last question,
11 mod 5 = 1and16 mod 5 = 1, so the keys collide. A complete answer should then state that rehashing is applied to handle the collision. If a particular rehashing calculation is not supplied, explain the mechanism without claiming a specific unsupported formula.
Ready to practise?
Choose your next step
Use the study guide for understanding, then switch into an active revision mode.
Related topics
