Chaining and open addressing
The two ways to resolve collisions: chaining (one linked list per slot) and open addressing (linear probing, quadratic probing, double hashing). Insert, search and delete in each, probe sequences and when they cover the whole table, primary and secondary clustering, why deletion in open addressing needs a tombstone, and how to decide whether a given table could have been produced by a sequence of insertions.
Why it exists
Two keys with the same hash value must both be stored and both be found later. There are two basic ideas. Chaining gives every slot a list, so collisions simply share a list. Open addressing keeps all keys in the table itself and, when the home slot is taken, probes other slots by a fixed rule. Chaining is simple and degrades gracefully; open addressing saves the pointers and has good cache behaviour but fails if the table fills up and needs care when deleting. GATE asks about the exact table after a sequence of insertions, about probe sequences, and about what goes wrong with deletion.
Prerequisites
You need hash functions, collisions and the load factor (hashing) and linked lists (linked lists).
Precise definitions
Chaining (separate chaining). Slot of the table holds the head of a linked list of all keys with .
- Insert: compute and put the new node at the front of that list: (after checking that the key is not already present, if duplicates are not allowed, which costs a search).
- Search: walk down the list of slot comparing keys.
- Delete: find the node and unlink it (a doubly linked list gives once the node is found).
Open addressing. At most one key per slot, so always. A probe sequence says which slots to try for key . For correctness it must be a permutation of (every slot is eventually tried).
- Insert: try the slots of the probe sequence until one is empty (or deleted).
- Search: try the slots in order until the key is found (success) or an empty slot is reached (failure: if the key were present it would have been placed there or earlier).
- Delete: the slot is marked DELETED (a tombstone), never just emptied.
The three standard probe sequences, with :
| Scheme | Remark | |
|---|---|---|
| Linear probing | visits all slots; clusters | |
| Quadratic probing | visits all slots only for special | |
| Double hashing | visits all slots iff |
Clustering. Primary clustering: in linear probing a run of occupied slots grows whenever a key hashes anywhere into it, so long runs get longer. Secondary clustering: in quadratic probing keys with the same home slot follow the same sequence. Double hashing avoids both because the step depends on the key.
How it works
Chaining in action
The animation inserts into buckets with and then searches for . The code is the real code; the picture is read from the real list nodes. New nodes go to the front, so stands before .
A hash table with chaining: key mod 11, one linked list per bucket
Real runC source
Buckets 0 to 10 (each bucket is a linked list; the newest key is at the front)
State
Blue = the position this step is working on, amber = a position being compared, red = positions just swapped, green = finished or in its final place, a filled blue cell = a value just placed, faded = not part of the structure at this step.
Step 1
An empty table of 11 buckets. Insert 45, 16, 29, 78, 33, 60, 14, 92, then search.
An unsuccessful search for (bucket ) walks the whole chain, comparisons; for (bucket , empty) it makes no comparison. The cost of a search is the length of one chain: on average, if all keys share a bucket. The table never becomes "full": may exceed .
Open addressing in action
The same eight keys, then , are inserted with linear probing; then is deleted and and are searched. A blank slot is empty, an x is a tombstone. Watch the keys and (home slots and already taken, so they move one slot on), and the eight probes of key : its home slot is the start of a run of occupied slots (slots to ), and it has to walk through all of them. The final search for the absent key (home slot as well) takes nine probes, because it stops only at the empty slot .
Open addressing with linear probing, and why deletion needs a tombstone
Real runC source
Table of 11 slots (blank = empty, x = deleted)
State
Blue = the position this step is working on, amber = a position being compared, red = positions just swapped, green = finished or in its final place, a filled blue cell = a value just placed, faded = not part of the structure at this step.
Step 1
An empty table of 11 slots, hash = key mod 11, linear probing (try the next slot). Insert 45, 16, 29, 78, 33, 60, 14, 92.
Why deletion needs a tombstone. After is deleted from slot , the key (home slot , stored in slot ) must still be found: its probe sequence is . If slot were set to empty, the search for would stop at slot ("an empty slot, so the key is absent") and report the wrong answer. A tombstone says "continue". Searches pass over tombstones; insertions may reuse them. The price: tombstones lengthen searches, so the table is rebuilt from time to time. (With chaining none of this arises.)
The three probe rules side by side
The program below inserts the same ten keys into an empty table of slots with the three rules (linear; quadratic with ; double hashing with , which lies between and and is coprime to the prime ) and prints the probes used by each key and the final table. It also counts how many different slots the first probes of one key visit.
#include <stdio.h>
#define M 11
#define EMPTY (-1)
static int table[M];
/* slot examined at probe number i (i = 0, 1, 2, ...) for the given key and scheme */
static int slot(int scheme, int key, int i) {
int h1 = key % M;
if (scheme == 0) return (h1 + i) % M; /* linear probing */
if (scheme == 1) return (h1 + i * i) % M; /* quadratic probing, h + i^2 */
int h2 = 1 + key % (M - 1); /* double hashing: step between 1 and 10 */
return (h1 + i * h2) % M;
}
int main(void) {
const char *name[3] = {"linear", "quadratic", "double"};
int keys[10] = {45, 16, 29, 78, 33, 60, 14, 92, 56, 23};
for (int s = 0; s < 3; s++) {
for (int b = 0; b < M; b++) table[b] = EMPTY;
printf("%-10s probes per key:", name[s]);
int total = 0;
for (int k = 0; k < 10; k++) {
int i = 0;
while (i < M && table[slot(s, keys[k], i)] != EMPTY) i++;
if (i == M) { printf(" fail"); continue; }
table[slot(s, keys[k], i)] = keys[k];
printf(" %d", i + 1);
total += i + 1;
}
printf(" (total %d)\n table:", total);
for (int b = 0; b < M; b++) {
if (table[b] == EMPTY) printf(" .");
else printf(" %2d", table[b]);
}
printf("\n");
}
printf("\nnumber of different slots visited by the first %d probes of the key 5:\n", M);
for (int s = 0; s < 3; s++) {
int seen[M] = {0}, count = 0;
for (int i = 0; i < M; i++) {
int b = slot(s, 5, i);
if (!seen[b]) { seen[b] = 1; count++; }
}
printf("%-10s %d of %d\n", name[s], count, M);
}
return 0;
}linear probes per key: 1 1 1 2 1 2 1 1 8 9 (total 27)
table: 33 45 78 14 92 16 60 29 56 23 .
quadratic probes per key: 1 1 1 2 1 2 1 1 4 fail (total 14)
table: 33 45 78 14 92 16 60 29 . . 56
double probes per key: 1 1 1 2 1 2 1 1 2 3 (total 15)
table: 33 45 . 14 92 16 60 29 56 23 78
number of different slots visited by the first 11 probes of the key 5:
linear 11 of 11
quadratic 6 of 11
double 11 of 11The same ten keys in three open addressing tables (m = 11): start
Computed by a script, not typed| scheme | probes for each key (in insertion order) | total probes | final table, slots 0 to 10 |
|---|---|---|---|
| linear probing | |||
| quadratic probing | |||
| double hashing |
Step 0
The keys 45, 16, 29, 78, 33, 60, 14, 92, 56, 23 are inserted in this order into an empty table of 11 slots with h(k) = k mod 11 and three probe rules. A dot is an empty slot.
Three observations. Linear probing used probes: the keys and both had to cross the run of occupied slots (probes and ). Double hashing needed only , because the step differs from key to key, so keys from the same run take different paths. Quadratic probing with failed to insert the last key although slots and were empty: its sequence visits only of the slots (the last line of the output) and all six were occupied. A probe sequence that is not a permutation of the slots can miss free slots.
When do the sequences cover the whole table?
- Linear probing: always.
- Double hashing: exactly when . Standard ways: prime and , or and odd.
- Quadratic probing: for prime, visits only slots; (that is, the triangular numbers) visits every slot when is a power of two. So the constants and must be chosen together.
Which tables are possible? For a table built by insertions only (no deletions) with linear probing, every key sits at a slot such that all slots from its home slot up to its own slot are occupied (otherwise it would have been placed earlier). This is a quick necessary test for "which of the following tables could result from inserting these keys" questions; the order of insertion decides which of two keys with the same home slot comes first.
Resizing. When grows too large the table is rebuilt with a larger (often doubled) and every key is rehashed, because depends on . The cost of rebuilding, , is spread over the insertions that led to it, so the amortized cost per insertion stays constant. Open addressing is usually resized at to ; chaining tolerates or more.
Worked examples
EX 1Build a chained table
Easy, , keys inserted at the front of the lists. Buckets: ; ; ; ; ; ; the others are empty. The longest chain has length , . If the keys are inserted in the opposite order, the buckets are the same but each chain is reversed: and .
EX 2Linear probing by hand
EasySame keys, linear probing. , , ; wants slot (taken), goes to ; ; wants (taken), goes to ; ; . Final table, slots to : . Probes per key: . A successful search of takes probes; an unsuccessful search for a key with home slot takes probes (slots to , ending at the empty slot ).
EX 3Double hashing
Medium, , . For : , . The sequence is . In the table of the program, went to slot after probes (slot was taken), where linear probing needed . The step is coprime to , so after probes all slots would have been visited.
EX 4A deletion trap
MediumLinear probing, , . Insert (slot ), (slot taken, goes to ), (slots , taken, goes to ). Delete by emptying slot . Search : probe slot (, no), slot is empty: the search stops and answers "absent", but is in slot . With a tombstone in slot the search continues to slot and finds it.
EX 5Which table is possible?
GATE level, , linear probing, keys inserted in the order . Simulate: ; ; ; : home (taken), (taken), (taken), : slot ; ; : home taken, , , taken, slot . Table: slots to hold . In any valid table every key must have all slots from its home slot to its position occupied: at slot with home needs slots to occupied: true. The table with and swapped (so in slot and in slot ) passes the same test but is not the result of this insertion order; it would result from inserting before . The test is necessary, not sufficient: the order of insertion must also be consistent.
Cheat box
Cheat box
- Chaining: list per slot, insert at the front , search expected, worst; can exceed ; no tombstones.
- Open addressing: all keys in the table, , probe sequence must be a permutation of the slots; search stops at an empty slot; delete = tombstone.
- Linear : all slots, primary clustering. Quadratic: secondary clustering, covers all slots only for special parameters ( with prime : slots). Double : all slots iff ; best distribution.
- Valid linear probing table: all slots between a key's home and its position are occupied.
- Resize when is large; rehash everything; amortized .
Traps and GATE patterns
Connections
Builds on
Across subjects
The hash functions and the idea of the load factor are in hashing; the expected costs of these schemes as a function of are in load factor and expected probes. Chaining uses linked lists and open addressing uses arrays.
PYQs
9 previous year questions from your book. I re-solved each one myself (9 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs
Consider a hash table P[0, 1, ..., 10] that is initially empty. The hash table is maintained using open addressing with linear probing. The hash function used is : h(x) = (x + 7) mod 11. Consider the following sequence of insertions performed on P: 1, 13, 22, 15, 11, 24 Which of the following positions in the hash table is/are empty after these insertions are performed? collisions. Let k be the number of keys, m be the number of slots in the hash table, and k>m. Which one of the following is the best hashing strategy to counteract the adversary?
- (a)Division method, i.e., use the hash function h(k) = k mod m
- (b)Universal hashing method.
- (c)Multiplication method, i.e., use the hash function h(k) = ⌊m(kA -⌊kA⌋)⌋ , where A is a carefully chosen constant.
- (d)If k is a prime number, use Division method. Otherwise, use Multiplication method.
- (a)0
- (b)10
- (c)2
- (d)1
The keys 5, 28, 19, 15, 26, 33, 12, 17, 10 are inserted into a hash table using the hash function h(k) = k mod 9. The collisions are resolved by chaining. After all the keys are inserted, the length of the longest chain is .
In a double hashing scheme, h1(k) = k mod 11 and h2(k) = 1 + (k mod 7) are the auxiliary hash functions. The size m of the hash table is 11. The hash function for the i-th probe in the open address table is [h1(k) + ih2(k)] mod m The following keys are inserted in the given order: 63, 50, 25, 79, 67, 24. The slot at which key 24 gets stored is . (Answer in integers)
Consider a double hashing scheme in which the primary hash function is h1(k) = k mod 23, and the secondary hash function is h2(k) = 1 + (kmod19). Assume that the table size is 23. Then the address returned by probe 1 in the probe sequence (assume that the probe sequence begins at probe 0) for key value k=90 is .
A hash table of length 10 uses open addressing with hash function h(k) = k mod 10, and linear probing. After inserting 6 values into an empty hash table, the table is as shown below How many different insertion sequences of the key values using the same hash function and linear probing will result in the hash table shown above?
- (a)10
- (b)20
- (c)30
- (d)
Which one of the following choices gives a possible order in which the key values could have been inserted in the table?
- (a)46, 42, 34, 52, 23, 33
- (b)34, 42, 23, 52, 33, 46
- (c)46, 34, 42, 23, 52, 33
- (d)42, 46, 33, 23, 34, 52
Consider a hash table with 9 slots. The hash function is h(k) = k mod 9. The collisions are resolved by chaining. The following 9 keys are inserted in the order: 5, 28, 19, 15, 20, 33, 12, 17, 10. The maximum, minimum, and average chain lengths in the hash table, respectively, are
- (a)3,0,and 1
- (b)3,3,and 3
- (c)4,0,and 1
- (d)3,0,and 2
The keys 12,18,13,2,3,23,5 and 15 are inserted into an initially empty hash table of length 10 using open addressing with hash function h(k) = k mod 10 and linear probing. What is the resultant hash table ?
- (a)0 1 2 2 3 2 4 5 1 6 7 8 1 9
- (b)
- (c). 0 1 2 12 3 13 4 2 5 3 6 23 7 5 8 18 9 15
- (d). 0 1 2 12,2 3 13,3,23 4 5 5,15 6 7 8 18 9
Consider a hash table of size seven, with starting index zero, and a hash function h(x) = (3x + 4) mod 7 . Assuming the hash table is initially empty, which of the following is the contents of the table when the sequence 1, 3, 8, 10 is inserted into the table using closed hashing? Note that "–" denotes an empty location in the table.
- (a)8, –, –, –, –, –, 10
- (b)1, 8, 10, –, –, –, 3
- (c)1, –, –, –, –, –, 3
- (d)1, 10, 8, –, –, –, 3 0 1 2 12 3 13 4 5 5 6 7 8 18 DATA STRUCTURE
Self-check
Question 1
Insert into a table of slots with using linear probing. Where does each key go?
Show answerHide
; (home taken) ; (home , then taken) .
Question 2
Why is a deleted slot in open addressing marked DELETED instead of EMPTY?
Show answerHide
A search stops at the first empty slot; emptying a slot would stop searches for keys that were placed beyond it in a probe sequence and make them seem absent.
Question 3
What condition makes double hashing visit every slot of the table?
Show answerHide
, for example prime with .
Question 4
Can a hash table with chaining hold more keys than it has slots?
Show answerHide
Yes: the lists simply get longer (load factor above ); open addressing cannot.
Revision card
One-screen revision
- Chaining: list per slot, front insertion, search , may exceed .
- Open addressing: ; probe sequence a permutation; stop at EMPTY; delete with tombstone.
- Linear: primary clustering. Quadratic: secondary clustering, parameters matter. Double: , .
- Valid table: slots from home to position occupied. Resize and rehash at high .
References
- ·Cormen, Leiserson, Rivest, Stein, Introduction to Algorithms (3rd edition), Chapter 11 Hash Tables (11.2 collision resolution by chaining, 11.4 open addressing)
- ·Sedgewick and Wayne, Algorithms (4th edition), Section 3.4 Hash Tables (separate chaining, linear probing)
- ·Knuth, The Art of Computer Programming, Volume 3, Section 6.4 Hashing
Verification
Both animations are real runs of the C code shown (chaining with insertion at the front of the list; linear probing with a tombstone). The C program on this page produced the probe counts and tables of the three open addressing schemes, which were replayed in Python. Verified by exhaustive checks in Python: linear probing visits every slot; double hashing visits every slot exactly when gcd(h2, m) = 1 (all m below 31); h + i^2 visits (m + 1)/2 different slots for the primes tested; the triangular numbers i(i+1)/2 visit every slot exactly when m is a power of two (all m below 70); the necessary condition on a linear probing table (every slot between a key's home and its position is occupied) held for 3000 random insertion sequences; and the failure of a search after a deletion that empties the slot was reproduced. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (loop counts, recurrences, merge and quicksort counts and averages, hash probes and load-factor formulas, DP tables, MST and shortest-path runs, Floyd matrices); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; The text said key 56 needs nine probes in the linear-probing animation; it needs eight (the nine belong to the later search for the absent key 100). Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)