Module 5 · 14 min read · Free chapter
Hashing
Hashing maps a key to a table slot, for example h(k) = k mod m with m prime, giving average Θ(1) search. Collisions are handled by chaining, where the load factor may exceed 1, or by open addressing with linear, quadratic or double-hashing probes. Linear probing suffers primary clustering, and an unsuccessful search needs about 1/(1 − α) probes.
Start from zero: letterboxes in a housing society
A big housing society has 2,000 flats but only 100 letterboxes at the gate. The postman puts each letter into the box numbered by the last two digits of the flat number, so a letter for flat 1203 goes into box 03. To collect mail, the family in flat 1203 opens only box 03 and never searches the other 99.
Sometimes two families share a box, because flat 503 also ends in 03. Then the box holds letters for both, and each family looks through that small pile. As long as the piles stay small, finding your letter takes just one or two looks.
This is hashing. A hash function turns a key, here the flat number, into a slot number, here the box number, by simple arithmetic. A hash table is the row of slots, and two keys landing in the same slot is called a collision.
Why it matters. Searching a sorted list needs about steps and an unsorted one needs , but a good hash table needs about one step whatever the size. That is why hashing is the engine behind dictionaries in Python, maps in Java, database indexes, compiler symbol tables and the login check on a website.
Hash functions: division and multiplication
A hash table has slots numbered to , and the hash function maps every key to one of them. A good hash function is fast to compute and spreads keys evenly, so that every slot is about equally likely. The ideal is uniform hashing, where each key is equally likely to land in any slot, independently of the others.
Direct addressing is the simplest table: use the key itself as the index. It is perfect when keys are small, like roll numbers 1 to 60, but a table for ten-digit mobile numbers would need slots. Hashing shrinks this to a table sized for the keys actually stored.
The division method. Take the remainder:
With , key goes to slot , because . The choice of matters a lot. If , the remainder is just the lowest bits of , so with the keys 32, 48 and 64 all land in slot ; similarly looks only at the last decimal digit.
So the usual advice is to choose as a prime not close to a power of 2. Such an makes the slot depend on all the digits of the key.
The multiplication method. Pick a constant with , multiply, keep only the fractional part, and scale it to the table size:
With and , key gives , whose fractional part is , so . A value close to is a common choice. Here the value of is not critical, so a power of is fine and makes the computation fast.
Why it matters. A poor hash function turns a table into a few long piles, and the promise is lost. GATE checks whether you can apply both methods correctly and whether you know why is a trap for division.
Collisions and separate chaining
Whenever there are more possible keys than slots, two different keys must sometimes share a slot. This is the pigeonhole principle, and it means collisions cannot be avoided, only handled. Under uniform hashing of keys into slots, the expected number of colliding pairs is , so even keys in slots give expected collisions.
Separate chaining keeps a linked list in each slot. A new key is hashed and inserted at the head of its chain in , and a search walks only the chain of its own slot. Take , , and insert 15, 11, 27, 8, 12 and 20:
- The key 15 goes to slot 1, 11 to slot 4 and 27 to slot 6.
- The key 8 also hashes to slot 1, so slot 1 now chains 8 → 15.
- The key 12 goes to slot 5, and 20 joins 27 in slot 6, giving 20 → 27.
The load factor is , the average chain length, which here is . With chaining may exceed , and the table still works, only more slowly. Under simple uniform hashing, an unsuccessful search walks a whole chain and takes expected time, where the counts computing the hash.
A successful search stops halfway through the chain on average, and needs about comparisons. So if grows in proportion to , stays constant and every operation is on average. The worst case, all keys in one slot, is still .
Worked example. A chained table has slots and three keys are inserted by uniform hashing. The probability that slots 0, 1 and 2 are all still empty is , because each key independently avoids those three slots with probability . GATE asks exactly this kind of probability question.
Why it matters. Chaining is simple, never "fills up" and handles deletions easily, so many library hash tables use it. Its cost is extra memory for pointers and poorer cache use, which is why open addressing is the main alternative.
Open addressing: linear, quadratic and double hashing
Open addressing stores every key inside the table itself, with no chains, so the load factor can never exceed . When the home slot is taken, the key probes a sequence of other slots until it finds an empty one. The -th probe, for , goes to slot , and a search follows the same sequence until it finds the key or an empty slot.
The three standard probe sequences are:
- Linear probing: , which tries the next slot, then the one after, and so on.
- Quadratic probing: , which jumps by growing steps; the simple form uses .
- Double hashing: , which jumps by a step that itself depends on the key.
Worked example. Take , , and for double hashing . Insert 22, 1, 13, 11, 24 and 33 in that order. The table gives the slot each key finally takes.
| Key | Home slot | Linear | Quadratic | Double |
|---|---|---|---|---|
| 22 | 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | 1 |
| 13 | 2 | 2 | 2 | 2 |
| 11 | 0 | 3 | 4 | 3 |
| 24 | 2 | 4 | 3 | 6 |
| 33 | 0 | 5 | 9 | 4 |
Follow key 33, whose home slot is . Linear probing tries 0, 1, 2, 3, 4 and finds slot 5 free, which takes 6 probes. Quadratic probing tries , , and then . Double hashing has , so it tries 0, 2 and then 4, which is free.
Two cautions come with these methods. Quadratic probing may miss a free slot even when the table is not full, but if is prime and it always finds one. In double hashing must never be and must be relatively prime to , which a prime guarantees, or the jumps would cycle through only part of the table.
Why it matters. Open addressing avoids pointers and keeps keys close together in memory, so it is fast in practice when the table is not too full. GATE's favourite hashing question gives exactly such a key sequence and asks for the final table or the slot of one key.
Clustering and deletion in open addressing
Look again at the linear-probing column. After six insertions, slots 0 to 5 form one solid run, and any new key whose home slot is anywhere from 0 to 6 will end up in slot 6. So slot 6 is filled next with probability , while slot 8 has only .
This is primary clustering: long runs of filled slots attract more keys and grow longer, and every key that lands in a run must walk to its end. In general, an empty slot just after a run of filled slots is filled next with probability . This snowball effect makes linear probing slow down sharply as the table fills.
Quadratic probing breaks up these runs, because it jumps away from the home slot by growing steps. But two keys with the same home slot still follow exactly the same sequence of jumps. This milder effect is called secondary clustering.
Double hashing removes both kinds almost completely, because two keys with the same home slot usually have different step sizes . A useful way to compare the three methods is to count their distinct probe sequences:
- Linear probing has only sequences, one for each starting slot.
- Quadratic probing also has only , since the start fixes the rest.
- Double hashing has about , one for each pair of start and step, which is much closer to ideal uniform hashing.
Deleting a key. In chaining you just remove the node from its list. In open addressing you cannot simply empty the slot, because a later search for a key placed beyond it would stop at the gap and wrongly report "not found".
So the slot is marked DELETED, a tombstone. A search treats it as occupied and keeps probing, while an insertion may reuse it. If many tombstones pile up, searches slow down, and the table is rebuilt by rehashing the remaining keys.
Why it matters. Clustering explains why real systems keep open-addressing tables well below full, often around three-quarters or less. The tombstone rule is a common trick question in GATE and in interviews.
Expected number of probes
How many probes does an open-addressing search really need? Under uniform hashing at load factor , each probe hits a filled slot with probability about . So an unsuccessful search, which stops at the first empty slot, needs on average at most
probes, which is , the expected wait for the first empty slot. A successful search for a key retraces the probes made when that key was inserted, and averaging over all keys gives at most .
Linear probing does worse because of clustering. The standard approximations for it are for an unsuccessful search and for a successful one. The table puts numbers on all four formulas.
| Load factor | Uniform, unsuccessful | Uniform, successful | Linear, unsuccessful | Linear, successful |
|---|---|---|---|---|
| 0.5 | 2 | 1.39 | 2.5 | 1.5 |
| 0.75 | 4 | 1.85 | 8.5 | 2.5 |
| 0.9 | 10 | 2.56 | 50.5 | 5.5 |
Worked example. At , uniform hashing needs probes for a miss and probes for a hit. At the miss costs probes under uniform hashing, but linear probing needs .
The lesson is that a half-full table is cheap under every method, but a nearly full one is not. Chaining behaves more gently, because its cost grows only linearly even when passes .
Why it matters. GATE asks the expected probes for a given as a quick numerical. The same numbers guide real design, telling you when to double the table and rehash every key.
Descriptive lens: how written papers ask this
Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about hashing in four ways:
- They ask you to define hashing, a hash function and a collision, and to describe good hash functions with examples.
- They ask you to insert a given sequence of keys using chaining and each open-addressing method, and to draw the final table.
- They ask you to compare chaining with open addressing, or linear with quadratic probing and double hashing, in terms of clustering and cost.
- They ask you to explain the load factor and its effect on performance, and how deletion is handled.
Practice questions (our own, not previous-year questions).
- Explain the division and multiplication methods of hashing, and say why the table size matters in the division method. (10 marks)
- Insert 22, 1, 13, 11, 24 and 33 into a table of size 11 using linear probing, quadratic probing and double hashing with . (10 marks)
- Compare separate chaining and open addressing, and explain primary and secondary clustering with an example. (15 marks)
Answer plan for question 3.
- Begin by stating that both methods handle collisions, which are unavoidable when keys outnumber slots.
- Describe chaining, in which each slot holds a list, the load factor may exceed , and a search costs on average.
- Describe open addressing, in which keys stay inside the table, the load factor stays at most , and a probe sequence finds the slot.
- Explain primary clustering with a short linear-probing example in which one run of slots keeps growing.
- Explain secondary clustering as keys with the same home slot following the same quadratic jumps, and note that double hashing avoids both.
- Compare memory, cache behaviour and deletion, noting that open addressing needs DELETED markers.
- Conclude that chaining suits unknown or high loads, while open addressing suits memory-tight tables kept well below full.
Quick revision
- A hash function maps keys to slots to , giving average search, insert and delete.
- The division method is , with a prime away from powers of .
- The multiplication method is , with near .
- Collisions are unavoidable, and keys in slots give about colliding pairs.
- Chaining has load factor , which may exceed , and an unsuccessful search costs .
- Open addressing keeps and uses linear, quadratic or double-hashing probe sequences.
- Linear probing causes primary clustering, quadratic probing causes secondary clustering, and double hashing avoids both.
- Uniform hashing needs at most probes for a miss and for a hit.
- Deletion in open addressing uses a DELETED marker, not an empty slot.
- In double hashing must be non-zero and relatively prime to .