Question
Consider a hash table with 9 slots. The hash function is . The collisions are resolved by chaining. The following 9 keys are inserted in the order: 5, 28, 19, 15, 20, 33, 12, 17, 10. The maximum, minimum, and average chain lengths in the hash table, respectively, are
Official answer and solution
- Official answer
- A
- From
- Official GATE 2014 answer key
Solution
Hash each key into the 9 slots using the modulo hash function and calculate the length of the collision chains.
Hash function: on 9 slots (0 to 8).
Compute hash slot for each of the 9 keys in order:
Distribution of keys across the 9 slots:
- Slot 0: empty (length 0)
- Slot 1: (length 3)
- Slot 2: (length 1)
- Slot 3: (length 1)
- Slot 4: empty (length 0)
- Slot 5: (length 1)
- Slot 6: (length 2)
- Slot 7: empty (length 0)
- Slot 8: (length 1)
Analyzing chain lengths:
- Maximum chain length: .
- Minimum chain length: .
- Average chain length: .
Thus, the maximum, minimum, and average chain lengths are 3, 0, and 1.
A
Computing average chain length only over non-empty slots () rather than over all 9 slots of the hash table.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2014 Computer Science and Information Technology (CS) Session 1
- Question
- 50 of 65, printed as CS Q.40
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.