Hashing MCQs for NSCT
Hashing stores and finds keys in expected O(1) time by using a hash function to turn each key into a slot index in a table. Collisions, when two keys get the same slot, are handled by chaining or open addressing. Hashing is topic 6 of the Data Structures & Algorithms area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 40.2% accuracy in this area, per the HEC NSCT report and APP.
Expect table-filling traces: insert these keys with linear probing, where does the last one go? Get those right and the theory questions follow. Hashing beats the O(log n) bound of binary search on average, because it does not compare keys to find a position.
Hash functions
A hash function h maps a key to a slot from 0 to m - 1, where m is the table size. A good one is deterministic, fast and spreads keys evenly.
| Method | Formula | Example |
|---|---|---|
| Division | h(k) = k mod m | h(50) = 50 mod 7 = 1 |
| Multiplication | h(k) = floor(m (kA mod 1)), A about 0.618 | m = 16, k = 100: floor(16 x 0.8034) = 12 |
| Folding | Split the key into parts and add them | 123 + 456 + 789 = 1368, mod 1000 = 368 |
| Polynomial (strings) | h = (h x 31 + code) mod m | h("CAT") mod 13 = 4 |
For the division method, choose m to be a prime that is not close to a power of 2. With m = 100, keys that are all multiples of 10 use only 10 slots; with m = 97 they use all 97. For the multiplication method the value of m is not critical, so a power of 2 is fine (CLRS).
Exam angle: a hash that adds character codes ignores order, so "listen" and "silent" always collide.
Collisions and load factor
A collision happens when two different keys hash to the same slot. Collisions cannot be avoided when there are more possible keys than slots (the pigeonhole principle), so every table needs a resolution strategy.
The load factor alpha = n/m (keys divided by slots) drives performance. Under uniform hashing the expected cost of an unsuccessful search is Theta(1 + alpha) with chaining and at most 1/(1 - alpha) probes with open addressing: 2 probes at alpha = 0.5 and 4 at 0.75. Tables usually double and rehash past a threshold such as 0.75, which keeps insertion O(1) amortised.
Exam angle: open addressing needs alpha <= 1; chaining does not.
Chaining
In separate chaining each slot holds a linked list of the keys that hash there. With m = 7 and keys 15, 11, 27, 8, 12, 33, 20, 6, slot 6 gets the chain 27, 20, 6 (length 3), while slots 1 and 5 hold two keys each.
Search is Theta(1 + alpha) on average, but O(n) in the worst case if every key lands in one chain. Deletion is simple: unlink the node.
Exam angle: for average successful-search cost, add up each key's position in its chain and divide by n.
Open addressing
In open addressing all keys live in the table itself, and a collision sends the key along a probe sequence:
| Scheme | Probe i | Weakness |
|---|---|---|
| Linear probing | (h(k) + i) mod m | Primary clustering: runs of full slots grow |
| Quadratic probing | (h(k) + i^2) mod m | Secondary clustering: same home slot, same sequence |
| Double hashing | (h1(k) + i h2(k)) mod m | h2 must never be 0 and should be coprime to m |
Inserting 50, 700, 76, 85, 92, 73, 101 into m = 7 with linear probing gives [700, 50, 85, 92, 73, 101, 76]. Searches stop at the first empty slot, so a deletion must leave a tombstone (DELETED marker); emptying the slot would hide keys placed beyond it.
Quadratic probing can fail on a bad table size: with m = 8, i^2 mod 8 only takes 0, 1 and 4, so a fourth key with home 0 cannot be placed even though five slots are empty. With a prime m and alpha <= 1/2 a free slot is always found.
Exam angle: with linear probing, a slot just after a cluster of length L is filled with probability (L + 1)/m, which is why clusters snowball.
Common traps
- Writing alpha = m/n. It is n/m, keys per slot.
- Thinking a perfect hash function can avoid all collisions for arbitrary keys. Only a known, fixed key set can be hashed perfectly.
- Forgetting to wrap around: probe (h(k) + i) mod m goes from slot m - 1 back to slot 0.
- Emptying a slot on delete in open addressing instead of marking it DELETED.
- Confusing primary clustering (linear probing) with secondary clustering (quadratic probing).
- Quoting O(1) as the worst case. It is the expected case; the worst case is O(n).
Worked examples
1. Understanding. Which open-addressing scheme avoids both primary and secondary clustering? Answer: double hashing. Its step size h2(k) depends on the key, so two keys with the same home slot usually follow different probe sequences.
2. Application. Insert 18, 29, 40, 7, 51 in that order into a table of size 11 with h(k) = k mod 11 and linear probing. Where does 51 go, and after how many probes? Answer: slot 0, after 5 probes. Every key hashes to 7. 18 takes 7, 29 takes 8, 40 takes 9 and 7 takes 10. 51 tries 7, 8, 9 and 10, then wraps around to slot 0.
3. Analysis. Keys 12, 22, 32 and 42 go into a table of size 10 with h(k) = k mod 10. Linear probing needs 1 + 2 + 3 + 4 = 10 probes in total. How many does quadratic probing need, and why? Answer: also 10. All four keys have home slot 2, and quadratic probing gives every key with the same home the same sequence (2, 3, 6, 11 mod 10 = 1). This is secondary clustering; double hashing with a key-dependent step would break it.
Practise
The quiz below has 100 Hashing questions, filterable by tier and difficulty.
Practise: 100 questions
FAQs
What is the difference between chaining and open addressing?
Chaining keeps colliding keys in a list attached to each slot, so the table can hold more keys than slots and deletion is easy. Open addressing stores every key inside the table and probes for another free slot on a collision. It saves pointer memory but needs alpha <= 1 and tombstones for deletion.
Why is the hash table size often a prime number?
With the division method h(k) = k mod m, a table size that shares factors with many keys sends those keys to only a few slots. A prime m spreads patterned keys, such as multiples of 10, across the whole table. Quadratic probing also needs a prime m to guarantee finding a free slot when alpha <= 1/2.
Is hash table search really O(1)?
On average, yes, provided the hash function spreads keys well and the load factor stays bounded by resizing. The worst case is O(n), when many keys collide into one chain or one long probe run. Randomised (universal) hash functions make that worst case very unlikely, even with adversarial input.
What is a good load factor for a hash table?
Many libraries resize at about 0.75 for chaining. Open-addressing tables often stay lower, around 0.5 to 0.7, because probe counts rise sharply as alpha approaches 1: 1/(1 - alpha) is 2 at 0.5 but 10 at 0.9.
Next step
Continue with Tree Algorithms MCQs, go back to the DSA MCQ hub, or test yourself across every area in an NSCT mock test.