Level 1 — RecognitionHashing

Hashing

20 minutes30 marksprintable — key stays hidden on paper

Chapter: 3.3 Hashing Time Limit: 20 minutes Total Marks: 30


Section A — Multiple Choice (1 mark each)

Choose the single best answer.

Q1. Which property is NOT a required property of a good hash function?

  • (a) Deterministic
  • (b) Uniform distribution
  • (c) Fast to compute
  • (d) Reversible (invertible)

Q2. In separate chaining, each bucket of the hash table typically stores:

  • (a) A single key–value pair only
  • (b) A linked list (or similar collection) of entries
  • (c) A tombstone marker
  • (d) A pointer to another hash table

Q3. The load factor α\alpha of a hash table with nn items and mm slots is defined as:

  • (a) α=m/n\alpha = m/n
  • (b) α=n/m\alpha = n/m
  • (c) α=nm\alpha = n \cdot m
  • (d) α=nm\alpha = n - m

Q4. In linear probing, after a collision at index ii, the next slot probed is:

  • (a) (i+1)modm(i + 1) \bmod m
  • (b) (i+i2)modm(i + i^2) \bmod m
  • (c) A slot given by a second hash function
  • (d) A random slot

Q5. Why are tombstone markers used when deleting from an open-addressing table?

  • (a) To free memory immediately
  • (b) To preserve probe sequences so later lookups still find shifted elements
  • (c) To increase the load factor
  • (d) To convert the table to chaining

Q6. Rehashing during a resize operation costs:

  • (a) O(1)O(1) every insertion
  • (b) O(logn)O(\log n)
  • (c) O(n)O(n) at the time of resize
  • (d) O(n2)O(n^2)

Q7. Hash table insertion is said to be amortized O(1)O(1) because:

  • (a) Every single insert is exactly constant time
  • (b) The occasional expensive resize is spread across many cheap inserts
  • (c) Resizing never happens
  • (d) It uses a balanced tree internally

Q8. In double hashing, the probe sequence uses:

  • (a) One hash function only
  • (b) A fixed step of 1
  • (c) A step size determined by a second hash function
  • (d) A quadratic step i2i^2

Q9. The key guarantee of universal hashing is that:

  • (a) Collisions are impossible
  • (b) The expected number of collisions is bounded regardless of input, by randomizing the hash choice
  • (c) The hash function is always the identity
  • (d) It removes the need for a hash table

Q10. Which application is most naturally solved with a hash set/map in O(n)O(n)?

  • (a) Sorting an array
  • (b) Two-sum: finding two numbers that add to a target
  • (c) Finding the shortest path in a graph
  • (d) Balancing a binary tree

Section B — Matching (1 mark each, Q11–Q15)

Match each term (Q11–Q15) to the correct description (A–E).

Term Description
Q11. Quadratic probing A. Uses a linked list per bucket
Q12. Chaining B. Probes (h(k)+c1i+c2i2)modm(h(k)+c_1 i + c_2 i^2)\bmod m
Q13. Load factor threshold C. Marks a deleted slot without breaking probe chains
Q14. Tombstone D. Point at which a resize is triggered (e.g. 0.75)
Q15. LRU cache E. Combines a hash map with ordering to evict least-recently-used

Section C — True/False WITH Justification (2 marks each: 1 T/F + 1 justification)

Q16. A hash function that returns a random value each time it is called is acceptable as long as it is fast.

Q17. In an open-addressing table, when the load factor approaches 1, the average probe length grows sharply.

Q18. Python's dict is implemented using separate chaining with linked lists.

Q19. Chaining allows the load factor to exceed 1, whereas standard open addressing cannot store more items than it has slots.

Q20. Frequency counting of characters in a string can be done in one pass using a hash map.


Answer keyMark scheme & solutions

Section A (1 mark each)

Q1 — (d) Reversible. Why: Hash functions need not be invertible; they map many keys to a small range, which is inherently many-to-one. Deterministic, uniform, and fast are the three core properties.

Q2 — (b) A linked list of entries. Why: Separate chaining resolves collisions by storing all colliding keys in a per-bucket list.

Q3 — (b) α=n/m\alpha = n/m. Why: Load factor is items per slot; it measures how full the table is.

Q4 — (a) (i+1)modm(i+1)\bmod m. Why: Linear probing steps one slot at a time (wrapping with mod).

Q5 — (b) To preserve probe sequences. Why: Physically emptying a slot would truncate a probe chain, making later keys unfindable; a tombstone keeps the chain traversable while marking the slot reusable.

Q6 — (c) O(n)O(n) at resize. Why: Every existing element must be rehashed into the new table.

Q7 — (b) Occasional resize spread over many cheap inserts. Why: Doubling capacity makes the O(n)O(n) resize occur rarely enough that the average per-insert cost is constant.

Q8 — (c) Step size from a second hash function. Why: Double hashing probes (h1(k)+ih2(k))modm(h_1(k)+i\cdot h_2(k))\bmod m, reducing clustering.

Q9 — (b) Expected collisions bounded by randomizing the hash choice. Why: A hash function chosen randomly from a universal family bounds expected collisions for any fixed input, defeating adversarial worst cases.

Q10 — (b) Two-sum. Why: Store seen numbers in a hash set and check for target - x in O(1)O(1) per element, giving O(n)O(n) total.

Section B — Matching (1 mark each)

Q11 → B (quadratic probing formula) Q12 → A (chaining = linked list per bucket) Q13 → D (threshold triggering resize) Q14 → C (tombstone marks deletion) Q15 → E (LRU cache = hash map + ordering)

Section C — True/False + Justification (2 marks each)

Q16 — FALSE. (1 mark) Justification (1 mark): A hash function must be deterministic; the same key must always map to the same slot, otherwise items could never be found again. Speed alone is insufficient.

Q17 — TRUE. (1 mark) Justification (1 mark): As α1\alpha \to 1, few empty slots remain, so probe sequences lengthen dramatically; for linear probing the expected probes grow roughly like 12(1+11α)\frac{1}{2}(1+\frac{1}{1-\alpha}), blowing up near 1.

Q18 — FALSE. (1 mark) Justification (1 mark): CPython's dict uses open addressing (a probe sequence within a contiguous array), not separate chaining with linked lists.

Q19 — TRUE. (1 mark) Justification (1 mark): In chaining, buckets hold lists, so nn can exceed mm (α>1\alpha>1). In open addressing each slot holds at most one item, so nmn \le m and resizing is required before it fills.

Q20 — TRUE. (1 mark) Justification (1 mark): Iterate once, incrementing map[char] for each character; each update is amortized O(1)O(1), giving one-pass O(n)O(n) counting.


Marks Summary

  • Section A: 10 marks
  • Section B: 5 marks
  • Section C: 15 marks (10 questions... 5 questions × 2 = 10) → Total = 25

(Note: Section C = 5×2 = 10 marks; A=10, B=5, C=10 → grand total 25. Adjust cover-page total to 25.)

[
  {"claim": "Load factor n/m with n=6 items, m=8 slots equals 0.75, the common resize threshold", "code": "n=6; m=8; alpha=Rational(n,m); result = (alpha == Rational(3,4))"},
  {"claim": "Linear probing expected probes for successful-ish estimate 0.5*(1+1/(1-alpha)) at alpha=0.75 exceeds probes at alpha=0.5", "code": "f=lambda a: Rational(1,2)*(1+1/(1-a)); result = f(Rational(3,4)) > f(Rational(1,2))"},
  {"claim": "Two-sum via hash set is O(n) which is asymptotically better than O(n log n) sort approach for large n", "code": "n=symbols('n', positive=True); result = simplify(limit(n/(n*log(n)), n, oo)) == 0"},
  {"claim": "Amortized doubling: total copy work for inserting N=1024 with doubling from 1 is < 2N", "code": "N=1024; total=sum(2**k for k in range(0,10)); result = total < 2*N"}
]