Computer Science and IT‹›⌂ 0:00 studied

Module 5 · 14 min read · Free chapter

Hashing

In short

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.

What are you preparing for? We show the practice and previous year questions of your exam.

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 log⁡2n\log_2 n steps and an unsorted one needs nn, 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 mm slots numbered 00 to m−1m - 1, and the hash function hh 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 101010^{10} slots. Hashing shrinks this to a table sized for the keys actually stored.

The division method. Take the remainder:

h(k)=k mod mh(k) = k \bmod m

With m=11m = 11, key 123123 goes to slot 123 mod 11=2123 \bmod 11 = 2, because 123=11×11+2123 = 11 \times 11 + 2. The choice of mm matters a lot. If m=2pm = 2^p, the remainder is just the lowest pp bits of kk, so with m=16m = 16 the keys 32, 48 and 64 all land in slot 00; similarly m=10m = 10 looks only at the last decimal digit.

So the usual advice is to choose mm as a prime not close to a power of 2. Such an mm makes the slot depend on all the digits of the key.

The multiplication method. Pick a constant AA with 0<A<10 < A < 1, multiply, keep only the fractional part, and scale it to the table size:

h(k)=⌊m⋅(kA mod 1)⌋h(k) = \lfloor m \cdot (kA \bmod 1) \rfloor

With A=0.618A = 0.618 and m=1000m = 1000, key 123123 gives kA=76.014kA = 76.014, whose fractional part is 0.0140.014, so h(123)=⌊14.0⌋=14h(123) = \lfloor 14.0 \rfloor = 14. A value close to A=5−12≈0.618A = \dfrac{\sqrt{5} - 1}{2} \approx 0.618 is a common choice. Here the value of mm is not critical, so a power of 22 is fine and makes the computation fast.

Why it matters. A poor hash function turns a table into a few long piles, and the Θ(1)\Theta(1) promise is lost. GATE checks whether you can apply both methods correctly and whether you know why m=2pm = 2^p 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 nn keys into mm slots, the expected number of colliding pairs is n(n−1)2m\dfrac{n(n-1)}{2m}, so even 1010 keys in 100100 slots give 90200=0.45\dfrac{90}{200} = 0.45 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 Θ(1)\Theta(1), and a search walks only the chain of its own slot. Take m=7m = 7, h(k)=k mod 7h(k) = k \bmod 7, 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 α=nm\alpha = \dfrac{n}{m}, the average chain length, which here is 67\dfrac{6}{7}. With chaining α\alpha may exceed 11, and the table still works, only more slowly. Under simple uniform hashing, an unsuccessful search walks a whole chain and takes Θ(1+α)\Theta(1 + \alpha) expected time, where the 11 counts computing the hash.

A successful search stops halfway through the chain on average, and needs about 1+α21 + \dfrac{\alpha}{2} comparisons. So if mm grows in proportion to nn, α\alpha stays constant and every operation is Θ(1)\Theta(1) on average. The worst case, all keys in one slot, is still Θ(n)\Theta(n).

Worked example. A chained table has 100100 slots and three keys are inserted by uniform hashing. The probability that slots 0, 1 and 2 are all still empty is (97100)3≈0.913\left(\dfrac{97}{100}\right)^3 \approx 0.913, because each key independently avoids those three slots with probability 97100\dfrac{97}{100}. 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 11. When the home slot is taken, the key probes a sequence of other slots until it finds an empty one. The ii-th probe, for i=0,1,2,…i = 0, 1, 2, \dots, goes to slot h(k,i)h(k, i), and a search follows the same sequence until it finds the key or an empty slot.

The three standard probe sequences are:

  • Linear probing: h(k,i)=(h1(k)+i) mod mh(k, i) = (h_1(k) + i) \bmod m, which tries the next slot, then the one after, and so on.
  • Quadratic probing: h(k,i)=(h1(k)+c1i+c2i2) mod mh(k, i) = (h_1(k) + c_1 i + c_2 i^2) \bmod m, which jumps by growing steps; the simple form uses (h1(k)+i2) mod m(h_1(k) + i^2) \bmod m.
  • Double hashing: h(k,i)=(h1(k)+i⋅h2(k)) mod mh(k, i) = (h_1(k) + i \cdot h_2(k)) \bmod m, which jumps by a step that itself depends on the key.

Worked example. Take m=11m = 11, h1(k)=k mod 11h_1(k) = k \bmod 11, and for double hashing h2(k)=7−(k mod 7)h_2(k) = 7 - (k \bmod 7). 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 00. Linear probing tries 0, 1, 2, 3, 4 and finds slot 5 free, which takes 6 probes. Quadratic probing tries 00, 0+1=10 + 1 = 1, 0+4=40 + 4 = 4 and then 0+9=90 + 9 = 9. Double hashing has h2(33)=7−5=2h_2(33) = 7 - 5 = 2, 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 mm is prime and α≤12\alpha \le \dfrac{1}{2} it always finds one. In double hashing h2(k)h_2(k) must never be 00 and must be relatively prime to mm, which a prime mm 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 711\dfrac{7}{11}, while slot 8 has only 111\dfrac{1}{11}.

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 rr filled slots is filled next with probability r+1m\dfrac{r+1}{m}. 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 h2(k)h_2(k). A useful way to compare the three methods is to count their distinct probe sequences:

  • Linear probing has only mm sequences, one for each starting slot.
  • Quadratic probing also has only mm, since the start fixes the rest.
  • Double hashing has about m2m^2, 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 α<1\alpha < 1, each probe hits a filled slot with probability about α\alpha. So an unsuccessful search, which stops at the first empty slot, needs on average at most

11−α\frac{1}{1-\alpha}

probes, which is 1+α+α2+…1 + \alpha + \alpha^2 + \dots, 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 1αln⁡11−α\dfrac{1}{\alpha} \ln \dfrac{1}{1-\alpha}.

Linear probing does worse because of clustering. The standard approximations for it are 12(1+1(1−α)2)\dfrac{1}{2}\left(1 + \dfrac{1}{(1-\alpha)^2}\right) for an unsuccessful search and 12(1+11−α)\dfrac{1}{2}\left(1 + \dfrac{1}{1-\alpha}\right) 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 α=0.5\alpha = 0.5, uniform hashing needs 11−0.5=2\dfrac{1}{1 - 0.5} = 2 probes for a miss and 10.5ln⁡2=2×0.693≈1.39\dfrac{1}{0.5} \ln 2 = 2 \times 0.693 \approx 1.39 probes for a hit. At α=0.9\alpha = 0.9 the miss costs 10.1=10\dfrac{1}{0.1} = 10 probes under uniform hashing, but linear probing needs 12(1+100)=50.5\dfrac{1}{2}(1 + 100) = 50.5.

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 1+α1 + \alpha grows only linearly even when α\alpha passes 11.

Why it matters. GATE asks the expected probes for a given α\alpha 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:

  1. They ask you to define hashing, a hash function and a collision, and to describe good hash functions with examples.
  2. They ask you to insert a given sequence of keys using chaining and each open-addressing method, and to draw the final table.
  3. They ask you to compare chaining with open addressing, or linear with quadratic probing and double hashing, in terms of clustering and cost.
  4. 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).

  1. Explain the division and multiplication methods of hashing, and say why the table size matters in the division method. (10 marks)
  2. Insert 22, 1, 13, 11, 24 and 33 into a table of size 11 using linear probing, quadratic probing and double hashing with h2(k)=7−(k mod 7)h_2(k) = 7 - (k \bmod 7). (10 marks)
  3. 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 11, and a search costs Θ(1+α)\Theta(1 + \alpha) on average.
  • Describe open addressing, in which keys stay inside the table, the load factor stays at most 11, 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 00 to m−1m - 1, giving average Θ(1)\Theta(1) search, insert and delete.
  • The division method is h(k)=k mod mh(k) = k \bmod m, with mm a prime away from powers of 22.
  • The multiplication method is h(k)=⌊m⋅(kA mod 1)⌋h(k) = \lfloor m \cdot (kA \bmod 1) \rfloor, with AA near 0.6180.618.
  • Collisions are unavoidable, and nn keys in mm slots give about n(n−1)2m\dfrac{n(n-1)}{2m} colliding pairs.
  • Chaining has load factor α=nm\alpha = \dfrac{n}{m}, which may exceed 11, and an unsuccessful search costs Θ(1+α)\Theta(1 + \alpha).
  • Open addressing keeps α≤1\alpha \le 1 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 11−α\dfrac{1}{1-\alpha} probes for a miss and 1αln⁡11−α\dfrac{1}{\alpha} \ln \dfrac{1}{1-\alpha} for a hit.
  • Deletion in open addressing uses a DELETED marker, not an empty slot.
  • In double hashing h2(k)h_2(k) must be non-zero and relatively prime to mm.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 14 answered

  1. With a good hash function, the average time to search a hash table is:

  2. Using h(k)=k mod 11h(k) = k \bmod 11, the key 123123 goes to slot:

  3. For the division method, which table size mm is the poorest choice?

  4. With the multiplication method, A=0.618A = 0.618 and m=1000m = 1000, the key 123123 hashes to:

  5. Ten keys are hashed uniformly into 100100 slots. The expected number of colliding pairs is:

  6. In separate chaining with nn keys and mm slots, the load factor α\alpha is:

  7. A chained table has 100100 slots and 33 keys are inserted by uniform hashing. The probability that slots 0, 1 and 2 all stay empty is:

  8. Table size 1111, h(k)=k mod 11h(k) = k \bmod 11, linear probing. After inserting 22, 1, 13, 11, 24, the key 33 is placed in slot:

  9. Same keys with quadratic probing (h(k)+i2) mod 11(h(k) + i^2) \bmod 11. Where does 33 go?

  10. Primary clustering is a problem mainly of:

  11. How many distinct probe sequences does double hashing give in a table of size mm?

  12. Under uniform hashing with open addressing at α=0.75\alpha = 0.75, the expected probes for an unsuccessful search are at most:

  13. In open addressing, a deleted key's slot is marked DELETED instead of EMPTY because:

  14. In double hashing h(k,i)=(h1(k)+i⋅h2(k)) mod mh(k, i) = (h_1(k) + i \cdot h_2(k)) \bmod m, the function h2h_2 must:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

Why should m be prime in the division method?

A prime m away from powers of 2 makes the slot depend on all digits of the key; m = 2ᵖ uses only the lowest p bits.

What is the load factor of a hash table?

It is α = n/m, the number of keys per slot; it can exceed 1 with chaining but never with open addressing.

What is the difference between primary and secondary clustering?

Primary clustering is the growth of long filled runs under linear probing; secondary clustering is keys with the same home slot sharing one quadratic sequence.

Why are deleted slots marked in open addressing?

A search stops at an empty slot, so emptying a deleted slot would hide keys stored beyond it; a DELETED marker avoids this.

More in this module: Data structures

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Programming, Data Structures and Algorithms (CMI, 106106145): hashing and dictionaries