Chapter 14: Hash Tables

Data Structures and Algorithms with C · Study Questions
The hash ideaHash functionsOpen addressingChainingThe deleted markerLoad factorResizing
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · The Hash Idea

Question 1: The Weak Point of a Hash Table

Which of the following operations can a hash table not do in O(1) on average?

B: A hash function scatters nearby keys into distant buckets; the order information is lost. A range query scans the whole table: O(n + m). If ordered traversal and range queries are needed, a balanced search tree is chosen.

Multiple Choice · Hash Functions

Question 2: Division with m = 64

For integer keys, h(k) = k mod 64 is used. Which part of the key does h(k) depend on?

D: Since 64 = 26, k mod 64 is the last 6 bits of k in binary; the upper bits are never used. Patterned keys with identical low bits (e.g. all multiples of 64) fall into the same bucket. That is why m is chosen as a prime that is not close to a power of 2.

Output Question · Hash Functions

Question 3: Anagrams and String Hashing

What does the following program print? (ASCII: a = 97, d = 100, l = 108)

unsigned str_hash(const char *s, unsigned m) {
  unsigned h = 0;
  for (; *s != '\0'; s++)
    h = (h * 31 + (unsigned char)*s) % m;
  return h;
}
unsigned char_sum(const char *s, unsigned m) {
  unsigned h = 0;
  for (; *s; s++) h += (unsigned char)*s;
  return h % m;
}
/* in main */
printf("%u %u %u %u\n",
  str_hash("dal", 13), str_hash("lad", 13),
  char_sum("dal", 13), char_sum("lad", 13));
Show answer

Output: 12 9 6 6

  • "dal": 100 % 13 = 9 → (9 · 31 + 97) % 13 = 376 % 13 = 12 → (12 · 31 + 108) % 13 = 480 % 13 = 12.
  • "lad": 108 % 13 = 4 → (4 · 31 + 97) % 13 = 221 % 13 = 0 → (0 + 100) % 13 = 9.
  • Sum: both are 305 → 305 % 13 = 6.

The sum ignores the position of the characters; anagrams always collide. In a polynomial hash each character is multiplied by a different power of 31 depending on its position.

Classic Question · Hash Functions

Question 4: The Folding Method

The key x = 123456789 is split into three-digit parts. What values do shift folding and boundary folding give? If the table size is m = 1000, what are the home addresses?

Show answer
  • Parts: 123 | 456 | 789.
  • Shift folding: the parts are added as they are: 123 + 456 + 789 = 1368 → 1368 mod 1000 = 368.
  • Boundary folding: every second part is reversed, as if the paper were folded (456 → 654): 123 + 654 + 789 = 1566 → 566.

Since the sum exceeds the table size, a mod m is taken as well. Folding brings every digit of the key into the result; in this respect it is better than division with m = 1000, which looks only at the last digits.

Multiple Choice · Hash Functions

Question 5: The Multiplication Method

With the multiplication method from the slides (s = 2654435769 ≈ 0.618 · 232), what do h_mul(1, 4) and h_mul(2, 4) return?

uint32_t h_mul(uint32_t k, int p) {
  const uint32_t s = 2654435769u;
  return (uint32_t)(k * s) >> (32 - p);
}

A: p = 4 → the top 4 bits are taken: 2654435769 >> 28 = 9 (2654435769 / 228 ≈ 9.9). 2 · s = 5308871538 overflows; modulo 232 it is 1013904242, and >> 28 = 3. The result lies in 0..15 (18 is impossible); doubling the key does not double the hash.

Step-by-Step Trace · Open Addressing

Question 6: Insertion with Quadratic Probing

14, 25, 36, 47 have been inserted into a table of M = 11 slots with quadratic probing (all four have home address 3). Now insert_quadratic(58) is called. Which slots are examined, and where does 58 end up?

04712143254563678910

Occupied slots: 1 (47), 3 (14), 4 (25), 7 (36).

04712143i254563678910

58 % 11 = 3 → home 3; d = 0: slot 3 is occupied (14).

04712143254i563678910

d = 1: 3 + 1 = 4 → occupied (25).

0471214325456367i8910

d = 2: 3 + 4 = 7 → occupied (36).

0471i2143254563678910

d = 3: (3 + 9) % 11 = 1 → occupied (47).

0471214325456367588i910

d = 4: (3 + 16) % 11 = 8 → empty: 58 goes to slot 8, 5 probes. Every key with the same home address follows the sequence 3, 4, 7, 1, 8 … (secondary clustering).

#define M 11
int insert_quadratic(int k) {
  int home = k % M;
  for (int d = 0; d < M; d++) {
    int i = (home + d * d) % M;
    if (table[i].state != USED) {
      table[i].key = k;
      table[i].state = USED;
      return i;
    }
  }
  return -1;          /* no place found */
}

Multiple Choice · Open Addressing

Question 7: Double Hashing Probe Sequence

If m = 13, h₁(k) = k mod 13, h₂(k) = 1 + (k mod 11) and h(k, i) = (h₁(k) + i · h₂(k)) mod 13, which slots do the first four probes for k = 50 visit?

C: h₁ = 50 mod 13 = 11, h₂ = 1 + 50 mod 11 = 1 + 6 = 7. The slots: 11, 18 mod 13 = 5, 25 mod 13 = 12, 32 mod 13 = 6. A is linear probing; B uses a step of 9 (4 back at every probe); D forgets the 1 + term in h₂ and uses a step of 6. Since 13 is prime, a step of 7 visits all 13 slots.

Spot the Bug · Open Addressing

Question 8: The Search That Stops at a Deleted Marker

27, 16, 5, 39, 17 have been inserted in this order into a linear-probing table (M = 11), and then erase(16) was called. If search is written as below, what do search(39) and search(5) return? What is the bug?

int search(int k) {
  int i = k % M;
  for (int d = 0; d < M; d++, i = (i + 1) % M) {
    if (table[i].state != USED) return -1;
    if (table[i].key == k) return i;
  }
  return -1;
}
Show answer

The table: 5: 27 · 6: deleted (16) · 7: 5 · 8: 39 · 9: 17.

012342751665739817910
  • search(39): home 39 % 11 = 6 → the slot is DELETED, != USED is true → −1. Yet 39 is in slot 8.
  • search(5): home 5 → 27, a different key → slot 6 is DELETED → −1. Yet 5 is in slot 7.

The search must stop only at an EMPTY slot; the deleted marker says "someone was here once, keep going":

if (table[i].state == EMPTY) return -1;
if (table[i].state == USED &&
    table[i].key == k) return i;

Multiple Choice · Separate Chaining

Question 9: Order in the Chain

3, 8, 13, 18 are inserted, in this order, into an empty chained table with MZ = 5 buckets using the slides' chain_insert. If the chain of bucket 3 is traversed from head to tail, which order is printed?

D: All four keys have bucket 3 (k % 5). chain_insert puts the new node at the head of the list (O(1)); the most recently inserted key comes first. Appending at the tail would preserve the order, but it would require walking to the end of the chain every time.

Step-by-Step Trace · Separate Chaining

Question 10: Deleting from the End of a Chain

The chain of bucket 3 is 18 → 33 → 8 and chain_erase(8) is called. How many iterations does the loop make, which pointer is updated, and what happens to the memory?

bkt[3]18338NULL

The chain: 18 → 33 → 8 → NULL.

bkt[3]18p338NULL

i = 8 % 5 = 3; p points to the first node, 18, and prev to NULL.

bkt[3]18prev33p8NULL

18 ≠ 8 → prev = 18, p = 33.

bkt[3]1833prev8pNULL

33 ≠ 8 → prev = 33, p = 8.

bkt[3]1833prevNULL8free

8 == 8: the loop ends after 2 iterations. prev is not NULL → 33->next = p->next (NULL). 8 is no longer in the chain.

bkt[3]1833NULL

free(p): the node is given back. p still holds the old address (a dangling pointer) but it is never used again.

bool chain_erase(int k) {
  int i = k % MZ;
  node *p = bucket[i], *prev = NULL;
  while (p != NULL && p->key != k) {
    prev = p;
    p = p->next;
  }
  if (p == NULL) return false;
  if (prev == NULL) bucket[i] = p->next;
  else prev->next = p->next;
  free(p);
  return true;
}

Spot the Bug · Separate Chaining

Question 11: Two Bugs in Deletion

The routine that deletes from a chain is written as below. Find the two bugs; in which case does the program crash, and in which case is the behavior undefined? Fix it.

bool chain_erase(int k) {
  int i = k % MZ;
  node *p = bucket[i], *prev = NULL;
  while (p != NULL && p->key != k) {
    prev = p;  p = p->next;
  }
  if (p == NULL) return false;
  free(p);
  prev->next = p->next;
  return true;
}
Show answer
  • Using freed memory: p->next is read after free(p). p is now a dangling pointer; its contents may be corrupted (undefined behavior).
  • The head node: if the sought key is in the first node of the chain, the loop is never entered and prev stays NULL; prev->next dereferences a null pointer and the program crashes. Moreover, bucket[i] is never updated.
if (p == NULL) return false;
if (prev == NULL) bucket[i] = p->next;
else prev->next = p->next;
free(p);       /* after the links are cut */
return true;

Coding Question · Separate Chaining

Question 12: Emptying the Table

Write void chain_clear(void), which gives back all the nodes of the chained table when the program ends. At the end every bucket must be NULL; no node may leak and no freed node may be read.

Show answer
void chain_clear(void) {
  for (int i = 0; i < MZ; i++) {
    node *p = bucket[i];
    while (p != NULL) {
      node *next = p->next; /* save it first */
      free(p);
      p = next;
    }
    bucket[i] = NULL;  /* no dangling head */
  }
}

next is saved in a local variable before the node is freed. Tested with a counting malloc/free wrapper, 4 insertions show 4 mallocs and 4 frees. The time is O(m + n).

Multiple Choice · Load Factor and Deletion

Question 13: Expected Number of Probes

In a linear-probing table the load factor is α = 2/3. By Knuth's formula, how many probes does an unsuccessful search take on average?

A: An unsuccessful search takes ½(1 + 1/(1 − α)²) = ½(1 + 1/(1/3)²) = ½(1 + 9) = 5. A successful search takes ½(1 + 1/(1 − α)) = ½(1 + 3) = 2. Under the uniform hashing assumption an unsuccessful search would take 1/(1 − α) = 3; the difference is the price of primary clustering.

Step-by-Step Trace · Load Factor and Deletion

Question 14: erase, insert and search Together

In a linear-probing table with M = 11, the keys 27, 16, 5, 39, 17 are present. erase(16), insert(28), search(17) are called in this order. How do the slots change, and how many probes does search(17) make?

012342751665739817910

27 home 5; 16 home 5 → 6; 5 home 5 → 7; 39 home 6 → 8; 17 home 6 → 9.

01234275i1665739817910

erase first finds the place with search(16): home 16 % 11 = 5 → 27, a different key.

01234275166i5739817910

16 is found in slot 6 (2 probes). The slot is not made EMPTY but DELETED.

01234275286i5739817910

28 % 11 = 6: the slot is DELETED, so not USED → 28 is written there; the deleted place is reused.

01234275286i5739817910

17 % 11 = 6 → 28 is there, a different key: continue.

0123427528657398i17910

Slots 7 (5) and 8 (39) hold different keys too.

0123427528657398179i10

17 in slot 9: 4 probes. If erase(16) had made the slot EMPTY and 28 had never been inserted, search(17) would stop at slot 6 and return −1.

int insert(int k) {
  int i = k % M;
  for (int d = 0; d < M; d++, i = (i + 1) % M)
    if (table[i].state != USED) {
      table[i].key = k; table[i].state = USED;
      return i; }
  return -1;
}
int search(int k) {
  int i = k % M;
  for (int d = 0; d < M; d++, i = (i + 1) % M) {
    if (table[i].state == EMPTY) return -1;
    if (table[i].state == USED &&
        table[i].key == k) return i;
  }
  return -1;
}
bool erase(int k) {
  int i = search(k);
  if (i < 0) return false;
  table[i].state = DELETED; return true;
}

Classic Question · Load Factor and Deletion

Question 15: Resizing

The keys 10, 17, 24, 4 are inserted, in this order, into an empty linear-probing table with m = 7 slots (h(k) = k mod m). After each insertion, if 2 * n > m, the table is grown to size next_prime(2 * m). When does the growth happen? In which slot is each key in the new table? Why can't the old array simply be copied as it is?

Show answer
  • 10 → slot 3; 17 → 3 is full → 4; 24 → 3, 4 are full → 5. n = 3: 6 > 7 is false.
  • 4 → home 4; 4, 5 are full → 6. n = 4: 8 > 7 → growth: next_prime(14) = 17.
  • The old slots are scanned from 0 to 6 and every key is inserted again: 10 → 10, 17 → 0, 24 → 7, 4 → 4. The new α = 4/17 ≈ 0.24.
  • The home address depends on m (k mod m). If the old indices were copied, 17 would stay in slot 4, search(17) would look at slot 0, see it empty and say "absent".

Growth takes O(n + m); because the size doubles each time, the cost per insertion is amortized O(1).

Multiple Choice · Summary

Question 16: Chaining or Open Addressing?

For a dictionary whose number of items cannot be predicted, with frequent deletions and no memory shortage, which is the best fit?

B: With chaining α > 1 is possible; the table does not fill up, it only slows down. Deletion is an ordinary linked-list deletion, and no deleted markers pile up. In open addressing, frequent deletion multiplies deleted markers and lengthens searches. Quadratic probing does have secondary clustering; direct addressing cannot be used unless the key universe is small.

Multiple Choice · Summary

Question 17: Malicious Input

A web server keeps the parameter names of requests in a hash table. An attacker sends thousands of names that all land in the same bucket, degrading operations to O(n) (hash flooding). Which is the most effective countermeasure?

C: In universal hashing the function is chosen at random each time the program runs; the attacker cannot compute in advance which keys will collide, and the expected cost stays O(1). As long as a fixed function is known (even with a prime m), colliding keys can be generated; changing the probing method does not prevent collisions either.

The End

Chapter 14: Hash Tables: Study Questions
Lecture slides: Chapter 14: Hash Tables · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.