GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Simulate chain lengths in a hash table

3 questions solved the same way, newest first. Back to Data Structures

How these are solved

Hash each supplied key modulo table length, collect equal residues into buckets, then read requested collisions or chain statistics. Extend existing group with 2004 collision-pair question.

1 mark · NAT
Report issue

Longest chain after hashing

Hashing

Question 30, GATE 2026: Insert keys 5,28,19,15,26,33,12,17,10 into a table with h(k)=k mod 9, resolving collisions by chaining. Find the longest chain length.
Tap to enlarge
Answer & solution

Official answer: 3

Step through it (11 steps)

Givens and goal

Insert keys 5,28,19,15,26,33,12,17,10 into a table with h(k)=k mod 9, resolving collisions by chaining. Find the longest chain length.

Approach

Group all keys by their remainder rather than by insertion adjacency.

Step-by-step solution

1. The nine remainders in order are 5,1,1,6,8,6,3,8,1. For example 28=3×9+1, 33=3×9+6, and 17=1×9+8.
2. Bucket 1 receives 28,19,10; bucket 3 receives 12; bucket 5 receives 5; bucket 6 receives 15,33; bucket 8 receives 26,17. Buckets 0,2,4,7 remain empty.
3. The nonempty chain lengths are therefore 3,1,1,2,2, summing to 9 as a consistency check with the number of inserted keys.
4. The maximum is 3. Whether chaining inserts at the head or tail changes order inside a bucket but not its length. Do not apply linear probing: the problem explicitly stores collisions in chains.

Original paper, page 22 · Answer key

2 marks · MCQ
Report issue

Hashing

Question 40, GATE 2014: A nine-slot hash table uses h(k)=k mod 9 and chaining. Insert keys 5,28,19,15,20,33,12,17,10 in that order. What are the maximum, minimum and average chain lengths over all slots? A: 3,0,1. B: 3,3,3. C: 4,0,1. D: 3,0,2.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step through it (11 steps)

1. Method and setup. Compute each key’s remainder, then count keys in each slot. Chaining does not probe or relocate a colliding key to another slot. Include empty slots when finding the minimum and when averaging: average chain length over the table is total keys divided by total slots.

2. Work through the result and its assumptions. The residues modulo 9 are 5, 1, 1, 6, 2, 6, 3, 8, 1. Slot1 has 3 keys; slots 0, 4, 7 are empty; all 9 slots together hold 9 keys, so average length 1. Maximum3, minimum 0, average 1: A.

Original paper, page 17 · Answer key

1 mark · MCQ
Report issue

Hash collisions under modulo ten

Hashing

Question 7, GATE 2004: Hash the input 4322,1334,1471,9679,1989,6171,6173,4199 using h(x)=x mod 10. Which statements hold? (1) 9679,1989,4199 collide. (2) 1471,6171 collide. (3) All inputs hash to the same value. (4) Every input has a different hash value.
Options:
(A) 1 only
(B) 2 only
(C) 1 and 2 only
(D) 3 or 4
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. The hash is the last decimal digit.9679,1989,4199 all map to 9, while 1471 and 6171 both map to 1. The remaining values map to 2,4,3.

2. Thus statements 1 and 2 only are true, C.

3. Teaching check: The eight hash values in input order are 2,4,1,9,9,1,3,9. This explicit list both verifies the two claimed collisions and refutes the “all equal” and “all different” alternatives. Collision means equal hash output, not equal original keys.

Original paper, page 1