Suppose we are given keys, hash table slots, and two simple uniform hash functions and . Further suppose our hashing scheme uses for the odd keys and for the even keys. What is the expected number of keys in a slot?
- (A)
- (B)
- (C)
- (D)
Suppose we are given keys, hash table slots, and two simple uniform hash functions and . Further suppose our hashing scheme uses for the odd keys and for the even keys. What is the expected number of keys in a slot?
ANSWER
The expected number of keys in a slot is — option (B).
Official answer: BSource: Official answer key, GATE 2022 CS (IIT Kharagpur)
Let the n keys be split into two disjoint groups by parity: n/2 keys hashed via h1 (a simple uniform hash function into m slots) and n/2 keys hashed via h2 (also simple uniform into the same m slots).
By definition of a simple uniform hash function, each key it processes lands in any fixed slot with probability 1/m, independently of the other keys in its own group.
Fix any one slot. For the h1-group: the expected number of its keys landing in this slot is , since each of the n/2 keys contributes probability 1/m via the indicator-expectation argument (linearity of expectation over indicator variables).
Similarly, for the h2-group: the expected number of its keys landing in this same slot is also .
By linearity of expectation, the total expected number of keys (from both groups combined) landing in the fixed slot is the sum of the two group expectations: .
This is exactly the same result as using a single simple uniform hash function on all n keys directly () — splitting the keys across two independent uniform hash functions by any fixed partition does not change the expected per-slot load, because linearity of expectation only requires that each individual key be uniformly distributed across slots, not that all keys share one hash function.
Hence the expected number of keys in a slot is , which is option (B).
KEY IDEA
Linearity of expectation applies regardless of which hash function each key uses: as long as every key independently lands in a uniformly random slot (among the m slots), the expected number of keys per slot is (total keys)/(total slots) = n/m, whether one hash function or several partition the keys.
WATCH OUT FOR
Assuming that splitting the keys across two independent hash functions by parity somehow doubles or halves the expected load (picking (C) or (D) ), instead of applying linearity of expectation separately to each half and adding.
From the GATE 2022 CS paper. Every question of this paper · Download the full paper · All 18 Sorting & hashing questions, with notes