Hashing MCQs for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. In a hash table, what is a collision?

    1. A key hashes to an index outside the table
    2. Two different keys hash to the same slot
    3. The same key is inserted twice
    4. The table's load factor exceeds 1
    Show answer

    Answer: B. Two different keys hash to the same slot

    A hash function maps a large key space onto m slots, so different keys can get the same index; that is a collision. Collision resolution (chaining or open addressing) decides where the second key goes.

  2. How is the load factor alpha of a hash table defined?

    1. alpha = m / n, slots divided by keys
    2. alpha = number of collisions / n
    3. alpha = longest chain length / m
    4. alpha = n / m, keys stored divided by table slots
    Show answer

    Answer: D. alpha = n / m, keys stored divided by table slots

    The load factor is the average number of keys per slot: n keys in m slots gives alpha = n/m. Expected search costs in both chaining and open addressing are expressed in terms of alpha.

  3. Under simple uniform hashing with chaining, what is the expected time for a successful search?

    1. Theta(1 + alpha)
    2. Theta(log n)
    3. Theta(alpha^2)
    4. Theta(n)
    Show answer

    Answer: A. Theta(1 + alpha)

    Each chain holds alpha keys on average. A search computes the hash in O(1) and then scans part of one chain; CLRS shows both successful and unsuccessful searches take Theta(1 + alpha). If m grows with n so alpha stays constant, that is O(1).

  4. In open addressing, what is the largest possible load factor?

    1. 1
    2. 0.5
    3. 2
    4. There is no limit
    Show answer

    Answer: A. 1

    Open addressing stores every key in the table itself, one key per slot, so n can never exceed m and alpha <= 1. Chaining keeps keys in lists outside the slots, so its alpha can exceed 1.

  5. Which collision-resolution method suffers most from primary clustering?

    1. Separate chaining
    2. Double hashing
    3. Linear probing
    4. Quadratic probing
    Show answer

    Answer: C. Linear probing

    Linear probing tries h, h + 1, h + 2, ..., so occupied slots join into long runs and any key hashing into a run lengthens it. That growth of contiguous runs is primary clustering. Quadratic probing and double hashing jump around and avoid it.

  6. Quadratic probing avoids primary clustering but suffers from secondary clustering. What is secondary clustering?

    1. Occupied slots merge into one long run
    2. Chains in some buckets grow longer than others
    3. The table must be rehashed twice as often
    4. Keys with the same home slot follow the same probe sequence
    Show answer

    Answer: D. Keys with the same home slot follow the same probe sequence

    In quadratic probing the sequence h(k) + i^2 depends only on the home slot h(k). Any two keys that collide at home therefore probe exactly the same slots in the same order. Double hashing fixes this because the step h2(k) depends on the key.

  7. In double hashing, probe i is (h1(k) + i * h2(k)) mod m. Which condition on h2 is required?

    1. h2(k) must equal h1(k)
    2. h2(k) must be greater than m
    3. h2(k) must never be 0 and should be relatively prime to m
    4. h2(k) must be even
    Show answer

    Answer: C. h2(k) must never be 0 and should be relatively prime to m

    If h2(k) = 0 every probe hits the same slot. If h2(k) shares a factor with m, the sequence visits only part of the table. A common design is a prime m with h2(k) = 1 + (k mod (m - 1)), or h2(k) = R - (k mod R) for a prime R < m.

  8. Why is the table size m usually a prime number when using the division method h(k) = k mod m?

    1. Prime numbers make k mod m faster to compute
    2. A prime m spreads keys with common patterns or factors across all slots
    3. A prime m guarantees there are no collisions
    4. Only a prime m lets the table be resized
    Show answer

    Answer: B. A prime m spreads keys with common patterns or factors across all slots

    If m shares a factor with many keys (for example m = 100 and keys that are multiples of 10), those keys land on only a few slots. A prime not close to a power of 2 depends on all bits of the key and spreads such patterns out. Collisions still happen.

  9. In open addressing, why are deleted slots marked with a special DELETED (tombstone) marker instead of being emptied?

    1. An empty slot would stop later searches early and hide keys placed beyond it
    2. To keep the load factor below 0.5
    3. Because keys cannot be removed from memory
    4. To make insertion skip the slot forever
    Show answer

    Answer: A. An empty slot would stop later searches early and hide keys placed beyond it

    Searches stop at the first empty slot. If a key in the middle of a probe sequence were simply erased, keys inserted after it along that sequence would become unreachable. A tombstone tells search to keep going, while insertion may reuse the slot.

  10. What is the worst-case time to search a hash table that uses separate chaining with n keys?

    1. O(1)
    2. O(log n)
    3. O(n)
    4. O(n log n)
    Show answer

    Answer: C. O(n)

    If every key hashes to the same slot, one chain holds all n keys and a search may scan the whole list. The O(1) figure is only the average under uniform hashing.

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.