Level 2 — RecallHashing

Hashing

30 minutes40 marksprintable — key stays hidden on paper

Chapter: 3.3 Hashing Difficulty Level: 2 — Recall (definitions, standard problems, short derivations) Time Limit: 30 minutes Total Marks: 40


Instructions

  • Answer all questions.
  • Show working for numeric questions.
  • Use ...... notation where mathematics is required.

Q1. State the three essential properties of a good hash function and give one sentence explaining each. (3 marks)

Q2. Define the load factor α\alpha of a hash table in terms of the number of stored elements nn and the number of buckets mm. If a chaining hash table has n=30n = 30 elements in m=8m = 8 buckets, compute α\alpha. (3 marks)

Q3. Briefly contrast chaining and open addressing as collision resolution strategies. List one advantage of each. (4 marks)

Q4. A hash table of size m=7m = 7 uses linear probing with hash function h(k)=kmod7h(k) = k \bmod 7. Insert the keys in this order: 50,700,76,85,92,7350, 700, 76, 85, 92, 73. Show the final contents of each slot (0066). (6 marks)

Q5. Explain why deletion in an open-addressing hash table cannot simply set the slot to empty. Describe the tombstone (deleted marker) technique and how it affects search and insert. (5 marks)

Q6. A hash table using double hashing has m=11m = 11, with h1(k)=kmod11h_1(k) = k \bmod 11 and h2(k)=1+(kmod10)h_2(k) = 1 + (k \bmod 10). Give the probe sequence (first three slots examined) for inserting key k=25k = 25. (4 marks)

Q7. Define amortized O(1)O(1) in the context of hash table insertion with resizing (doubling). In one or two lines, explain why occasional O(n)O(n) rehashing still gives O(1)O(1) amortized cost. (4 marks)

Q8. State the defining property of a universal family of hash functions HH. What probabilistic guarantee does choosing hh uniformly at random from HH provide for two distinct keys xyx \neq y? (4 marks)

Q9. Using a hash-based approach, describe how to solve the two-sum problem (find indices i,ji, j such that a[i]+a[j]=Ta[i] + a[j] = T) in a single pass. State the time complexity. (4 marks)

Q10. A Python dict and set are built on hash tables. State (i) what open-addressing scheme underlies CPython's dict, and (ii) one requirement a key must satisfy to be usable in a dict. (3 marks)


END OF PAPER

Answer keyMark scheme & solutions

Q1. (3 marks — 1 each)

  • Deterministic: the same key always hashes to the same value (else lookups fail). (1)
  • Uniform: distributes keys evenly across buckets to minimise collisions. (1)
  • Fast: computable in O(1)O(1) / constant time so operations stay efficient. (1)

Q2. (3 marks)

  • Definition: α=nm\alpha = \dfrac{n}{m}. (2)
  • Computation: α=308=3.75\alpha = \dfrac{30}{8} = 3.75. (1)

Q3. (4 marks)

  • Chaining: each bucket holds a linked list (or dynamic structure) of colliding entries; the table stores pointers to chains. (1)
  • Open addressing: all entries stored directly in the table array; collisions resolved by probing for another slot. (1)
  • Advantage of chaining: handles high load factors (α>1\alpha > 1) gracefully; simple deletion. (1)
  • Advantage of open addressing: better cache locality, no pointer/linked-list overhead. (1)

Q4. (6 marks) h(k)=kmod7h(k)=k\bmod 7:

  • 50mod7=150 \bmod 7 = 1 → slot 1
  • 700mod7=0700 \bmod 7 = 0 → slot 0
  • 76mod7=676 \bmod 7 = 6 → slot 6
  • 85mod7=185 \bmod 7 = 1 → occupied, probe 2 → slot 2
  • 92mod7=192 \bmod 7 = 1 → occupied 1,2 probe 3 → slot 3
  • 73mod7=373 \bmod 7 = 3 → occupied, probe 4 → slot 4

Final table (1 mark per correct slot placement, 6 total):

Slot 0 1 2 3 4 5 6
Key 700 50 85 92 73 76

Q5. (5 marks)

  • If we simply empty the slot, a later search that probed through that slot to reach a key beyond it would now terminate early (hit an empty slot) and wrongly report "not found". (2)
  • Tombstone: mark the slot with a special DELETED marker rather than empty. (1)
  • Search: treats a tombstone as "occupied, keep probing" — does not stop. (1)
  • Insert: may reuse a tombstone slot (treats it as available). Note: many tombstones degrade performance → periodic rehashing clears them. (1)

Q6. (4 marks)

  • h1(25)=25mod11=3h_1(25) = 25 \bmod 11 = 3. (1)
  • h2(25)=1+(25mod10)=1+5=6h_2(25) = 1 + (25 \bmod 10) = 1 + 5 = 6. (1)
  • Probe sequence: (h1+ih2)mod11(h_1 + i\cdot h_2)\bmod 11:
    • i=0i=0: 33
    • i=1i=1: (3+6)mod11=9(3+6)\bmod 11 = 9
    • i=2i=2: (3+12)mod11=15mod11=4(3+12)\bmod 11 = 15\bmod 11 = 4 (2 for correct first three: 3, 9, 4)

Q7. (4 marks)

  • Amortized O(1)O(1): the average cost per operation over a sequence of operations is constant, even if individual operations are occasionally expensive. (2)
  • With doubling, resizing nn elements costs O(n)O(n) but happens after n\sim n cheap inserts; the total cost of nn inserts is O(n)O(n) (geometric series 1+2+4++n=O(n)1+2+4+\dots+n = O(n)), so cost per insert =O(1)= O(1) amortized. (2)

Q8. (4 marks)

  • Universal family: HH is universal if for any two distinct keys xyx \neq y, PrhH[h(x)=h(y)]1m\Pr_{h \in H}[h(x) = h(y)] \le \frac{1}{m} where mm is the table size and hh is chosen uniformly at random from HH. (3)
  • Guarantee: the probability that two distinct keys collide is at most 1/m1/m, independent of the input, giving expected O(1)O(1) operations regardless of adversarial keys. (1)

Q9. (4 marks)

  • Maintain a hash map from value → index. Iterate through the array; for each a[i]a[i], check whether Ta[i]T - a[i] is already in the map. (2)
  • If found, return the stored index and ii; otherwise insert a[i]:ia[i]:i and continue. (1)
  • Time complexity: O(n)O(n) (each lookup/insert is expected O(1)O(1)). (1)

Q10. (3 marks)

  • (i) CPython's dict uses open addressing (a perturbation-based probe sequence), not chaining. (2)
  • (ii) A key must be hashable — implement __hash__ and be immutable/equality-consistent (equal keys must have equal hashes). (1)

[
  {"claim":"Load factor n=30, m=8 gives 3.75","code":"result = (Rational(30,8) == Rational(15,4)) and (float(Rational(30,8))==3.75)"},
  {"claim":"Linear probing final positions for keys 50,700,76,85,92,73 in m=7","code":"m=7\ntable=[None]*m\nfor k in [50,700,76,85,92,73]:\n    i=k%m\n    while table[i] is not None:\n        i=(i+1)%m\n    table[i]=k\nresult = table==[700,50,85,92,73,None,76]"},
  {"claim":"Double hashing probe sequence for k=25, m=11 is 3,9,4","code":"m=11\nk=25\nh1=k%m\nh2=1+(k%10)\nseq=[(h1+i*h2)%m for i in range(3)]\nresult = seq==[3,9,4]"},
  {"claim":"Geometric doubling sum 1+2+...+n is O(n): total < 2n","code":"n=1024\ntotal=0\ns=1\nwhile s<=n:\n    total+=s\n    s*=2\nresult = total < 2*n*2 and total==2047"}
]