Introduction
A dictionary stores a set of keys (each possibly with satellite data) and supports INSERT, SEARCH and DELETE. A sorted array answers SEARCH in time and a balanced search tree does all three in . Hashing does all three in expected time under reasonable assumptions, by computing where a key lives instead of searching for it.
The price is order. A hash table scatters keys on purpose, so it cannot report the minimum, the successor of a key or all keys in a range without examining the whole table.
This note covers direct addressing, the hash table, the load factor, the standard hash functions (division, multiplication, mid-square, folding, strings and universal hashing) and the probability that keys collide. What a table does after a collision (chaining, linear, quadratic and double probing, and their expected costs) is the subject of Collision Resolution.
Prerequisites
- Asymptotic Notation: expected versus worst case.
- Arrays: Address Calculation and Special Matrices: a table is an array indexed by slot number.
Core concepts
Universe, keys and slots
- The universe is the set of all possible keys, for example all 32-bit integers or all strings of length at most 20.
- The key set is the set actually stored, with . Usually .
- A hash table has slots. A hash function sends key to slot , the home slot or hash value of .
- A collision is a pair of distinct keys with .
Collisions are unavoidable. If , the pigeonhole principle forces at least two keys of into one slot, so no hash function avoids collisions for every possible key set. A good hash function only makes them rare and evenly spread; a collision-resolution method handles the ones that happen.
Slot numbering convention. Hash table slots are numbered to , because every standard hash function produces a remainder. Other arrays in these notes are 1-based.
Direct addressing
When is small, use a direct-address table with one slot per possible key: slot holds the element with key , or NIL.
Pseudocode uses <- for assignment, x.key for the key of element x, NIL for an empty slot, and 0-based slots .
DIRECT-ADDRESS-SEARCH(T, k)
1 return T[k]
DIRECT-ADDRESS-INSERT(T, x)
1 T[x.key] <- x
DIRECT-ADDRESS-DELETE(T, x)
1 T[x.key] <- NILEach operation is in the worst case, and space is . This is the right choice when is comparable to (for example, keys are roll numbers 0 to 999). When is huge and is small, the space is wasted, and hashing replaces by with slots.
Load factor and simple uniform hashing
The load factor of a table holding keys in slots is
With chaining can exceed 1; with open addressing always.
Simple uniform hashing is the assumption that each key is equally likely to hash to any of the slots, independently of where other keys hash. It is an idealisation (a fixed is deterministic), but it is the model in which expected costs such as for search with chaining are proved. Under it, two fixed distinct keys collide with probability exactly .
Expected versus worst case. Chaining under simple uniform hashing gives expected time per operation when . For open addressing, require for fixed and the appropriate uniform-hashing assumptions. The worst case is still per search: if all keys land in one slot, the table degenerates into a list.
What makes a hash function good
- Deterministic: the same key always gets the same slot.
- Fast: for a fixed-size key, for a string of length .
- Uses all of the key: every bit or digit of the key should affect , so that keys differing only in some positions still spread.
- Spreads the actual key set evenly over all slots, including keys with patterns (all even, all multiples of 10, common prefixes).
Hash functions
Division method
One division, so it is fast. Its quality depends entirely on .
- Avoid . Then is the lowest bits of , and the other bits are ignored. With , the keys all hash to slot 0.
- Avoid for decimal keys, for the same reason: only the last digits count.
- Choose prime, not close to a power of 2. With the same five keys go to slots .
EDGE CASE. For negative keys, the mathematical lies in (for example ), but C's % operator gives for -3 % 11. A question that uses C code means C's operator.
Multiplication method
Choose a constant with and set
where is the fractional part of . The value of is not critical, so a power of 2 is fine and convenient. The golden-ratio choice is the usual recommendation and works well in practice.
Fixed-point form (word size , ). Assume integer and a nonnegative -bit key. Let and . This word method implements multiplication hashing for the fixed-point constant , approximating the chosen real . Multiply the -bit key by to get a -bit product . Then is the most significant bits of the low word , that is .
HASH-MULTIPLY(k, s, w, p)
1 r <- k * s // 2w-bit product
2 r0 <- r mod 2^w // low word
3 return floor(r0 / 2^(w - p)) // top p bits of the low wordEvaluation costs. On a word model with constant-time sufficiently wide multiplication, masking and shifting, HASH-MULTIPLY takes time and auxiliary words in best, average and worst cases. A constant number of words must hold the exact -bit product (or a primitive must return its low word exactly); signed overflow is not a substitute. The input key and one-word hash output are excluded. These are word-operation costs, not bit-complexity bounds as grows.

Mid-square and folding
- Mid-square: square the key and take the middle digits (or bits). For , , and the middle three digits give for a table of 1000 slots. The middle digits depend on every digit of .
- Folding (shift folding): cut the key into equal parts and add them, then reduce modulo . For and 3-digit parts, , and .
Hashing strings
Evaluate the string polynomial modulo using Horner's rule. The stored is in ; before reduction, , where is the largest character code. The simpler bound requires every code to be less than . Choosing with ASCII is a polynomial hash, not an assertion that ASCII codes are base-31 digits:
HASH-STRING(c, L, R, m)
1 h <- 0
2 for i <- 0 to L - 1
3 h <- (h * R + code(c[i])) mod m
4 return hThe result equals , because reduction modulo commutes with addition and multiplication. Summing the character codes without the powers of is a poor hash: every anagram ("TEA", "EAT", "ATE") collides.
Evaluation costs. HASH-STRING takes time in best, average and worst cases, including the empty string, and auxiliary words excluding the input string and scalar hash output. Each character lookup, multiply-add and modulo must be constant time and exact at the chosen widths: an intermediate register must hold , or use an equivalent overflow-safe modular primitive. Arbitrary-precision arithmetic has additional bit costs; silent machine overflow need not preserve the hash.
Universal hashing
An adversary knowing a fixed may choose colliding keys when a bucket contains enough of them. By the pigeonhole principle, some bucket has at least keys; an all-colliding -set is guaranteed only for . The universal-hashing guarantee below is for a fixed key set chosen independently of the random hash. Universal hashing picks at random, at run time, from a family . The family is universal if for every pair of distinct keys ,
A standard universal family. Restrict this family to integer keys in , with prime and integer . Choose and uniformly and independently from their respective sets, and for , set
There are equally likely parameter pairs ; for some allowed and , different pairs represent the same map. For example, with , , . With chaining and a universal family, the expected chain length seen by a search is at most for every key set, because the randomness is in the choice of , not in the input.
Worked examples
Division method with collisions
Insert into a table with and .
| Key | 54 | 26 | 93 | 17 | 77 | 31 | 44 | 20 |
|---|---|---|---|---|---|---|---|---|
| 10 | 4 | 5 | 6 | 0 | 9 | 0 | 9 |
Two collisions occur: with at slot 0, and with at slot 9. The load factor is . Under simple uniform hashing the expected number of colliding pairs among 8 keys is , so two pairs is typical.

Multiplication method
For , and : , so and .
In fixed point with , () and (so ): , so , , and (Figure 1).
A string with Horner's rule
Hash "GATE" with ASCII codes (G = 71, A = 65, T = 84, E = 69), and :
| Step | Character | after the step |
|---|---|---|
| 1 | G | |
| 2 | A | |
| 3 | T | |
| 4 | E |
Check without intermediate reduction: , and .
How likely is a collision?
For integers and , the formula below applies; for , .
Under simple uniform hashing, the probability that keys in slots are all in different slots is
and the expected number of colliding pairs is (exact, by linearity of expectation over the pairs). With and , , so a collision is more likely than not: this is the birthday paradox. Collisions appear once is around , long before the table is full.
Two more quantities follow from the same independence.
- A given set of slots stays empty. A fixed slot misses one key with probability , so after insertions a fixed set of slots is still empty with probability . With and , the probability that slots 0, 1 and 2 are all still empty is . This form is a common GATE question.
- Expected number of empty slots is , by linearity over the slots. With and it is ; the division example above, with 8 keys in 6 distinct slots, leaves 5 empty, close to that value. When the fraction of empty slots tends to .
Mathematical explanation
Why a power of 2 is bad for the division method
Write with . Then , the last bits. Keys that agree in their low bits collide regardless of their high bits; for example, all multiples of go to slot 0. A prime far from a power of 2 makes depend on all bits of .
Common factors. If keys share a factor with (all keys even and even), then is also a multiple of , so only slots are ever used. With prime, for every .
Expected cost depends on , not on
Under simple uniform hashing with chaining, an unsuccessful search examines on average keys (the expected length of one chain) plus the cost of computing , so it takes time. If grows with so that (for example, double and rehash when passes a threshold), every operation is expected. Doubling makes the rehashing cost amortised per insertion, because the sizes form a geometric series. The full analysis, including open addressing, is in Collision Resolution.
Important properties and observations
| Operation | Direct addressing | Chaining, expected (simple uniform hashing, ) | Hashing, worst case | Balanced BST |
|---|---|---|---|---|
| SEARCH | ||||
| INSERT | with a duplicate check, else with chaining | |||
| DELETE | ||||
| MIN, successor, range | plus output | |||
| Space |
Common misconceptions.
- "A good hash function prevents collisions." No function can, once ; it can only spread them.
- "Hashing is ." Only in expectation, and for chaining with under simple uniform hashing; open addressing instead needs for fixed and suitable uniform-hashing assumptions. The worst case is .
- "The multiplication method needs a prime ." It does not; is the usual choice. The prime- advice is for the division method.
- "Universal hashing assumes random keys." It assumes nothing about the keys; the random choice of supplies the randomness.
Hashing versus search trees. Use a hash table for pure lookup by key. Use a search tree when the question needs order: sorted output, the minimum, the predecessor or successor, or a range query. A binary search tree answers those in time proportional to its height.
GATE examination perspective
- DIVISION ARITHMETIC. Compute carefully for every key, in the given order, then count collisions or occupied slots. Write the slot list before drawing anything.
- POWER-OF-TWO TABLE SIZE. Recognise that or with the division method keeps only the low bits or digits, so patterned keys cluster.
- MULTIPLICATION METHOD. Fractional part first, then multiply by , then floor. In the fixed-point form, take the top bits of the low word, not of the whole product.
- LOAD FACTOR. , keys over slots. With open addressing .
- PROBABILITY TRAPS. Two distinct keys collide with probability under simple uniform hashing; the expected number of colliding pairs among keys is ; the probability of no collision is the falling product , not .
- STRING HASHES. Reduce modulo at every Horner step; sums of character codes make all anagrams collide.
- EMPTY SLOTS. A fixed set of slots survives insertions empty with probability ; the expected number of empty slots is .
Summary
Sources
- T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009: Chapter 11, Sections 11.1 (direct-address tables), 11.2 (hash tables) and 11.3 (hash functions: division, multiplication and universal hashing).