Collision Handling
(Open Addressing)

Distribute Education like Computer Geek

Collision Handling Technique – Open Addressing

Imagine a classroom with fixed chairs. A student comes in. His seat number is 7. But seat 7 is already occupied. Now what?

In Separate Chaining, we add another chair beside it.

But in Open Addressing, we do something different. We say – “Find another empty seat inside the same classroom.” That is the core idea of Open Addressing in hashing.

Today, we will understand Open Addressing clearly – its types, algorithms, examples, time complexity, space complexity, and real applications. If you are preparing for GATE, UGC NET, placements, or interviews, this topic is extremely important.

.

What is Open Addressing?

Open Addressing is a collision handling technique in hashing where –

  • All elements are stored inside the hash table array itself.
  • If collision happens, we probe (search) for another empty slot using a specific probing technique.

No linked lists. No extra memory. Just smart searching. In this technique, load factor α must always be less than 1. Because once the table is full, insertion stops.

.

Types of Open Addressing

Collision Handling Techniques

There are three main searching (probing) techniques –

  1. Linear Probing
  2. Quadratic Probing
  3. Double Hashing

Each one tries to solve collision in a slightly smarter way.

  1. Linear Probing

“If collision occurs at index h(k), check the next index. If that is full, check the next. Continue until an empty slot is found.”

Simple. Straight. Like standing in a queue.

.

Algorithm – Linear Probing

Insertion

  1. Compute index = h(k)
  2. If slot is empty, insert
  3. Else check (index + 1) mod m
  4. Repeat until empty slot found

Searching

  1. Compute index = h(k)
  2. If key found, return
  3. Else check next slot
  4. Stop when empty slot found or key located

Example

Let m = 7 and Hash function h(k) = k mod 7

Insert keys – 10, 17, 24

10 mod 7 = 3 → index 3rd is empty → insert 10.
17 mod 7 = 3 → collision → check 4th index (empty) → insert 17
24 mod 7 = 3 → collision → check 4th index (full) → check 5th Index (empty) → insert 24

Table looks like –

Linear Probing - Collision handling using Open Addressing

You see a cluster forming. This is called Primary Clustering.

Time Complexity – Linear Probing

  • Best case – O(1)
  • Average case – O(1) when α small
  • Worst case – O(n)

As load factor increases, clusters grow. Performance degrades sharply.

Space Complexity – Linear Probing

The space complexity of Linear probing is O(m). There is no extra linked lists. It is only an array.

.

  1. Quadratic Probing

“In open addressing, when collision happens, we must find another empty place inside the same hash table. In quadratic probing, we do not move step by step. We move in square steps.”

Suppose a key is hashed to index h(k), and that position is already full. Then we check –

h(k) + 1²
h(k) + 2²
h(k) + 3²
and so on (mod m)

So actually we are jumping like +1, +4, +9, +16 …

Because of this square movement, elements do not form a long continuous cluster like in linear probing. They get more space and spread better inside the table.

In simple words, quadratic probing handles collision by checking positions at increasing square distances instead of checking every next index. It reduces clustering, but it still cannot remove it completely.

.

Algorithm – Quadratic Probing

Insertion –

  1. Compute h(k)
  2. For i = 0 to m-1:
    index = (h(k) + i²) mod m
  3. Insert at first empty slot

Searching –

Same probing sequence used until key found or empty slot encountered.

  1. Compute index = h(k)
  2. If key found, return
  3. Else check next slot
  4. Stop when empty slot found or key located

Example

Let m = 7 and H(k) = k mod 7

Insert – 10, 17, 24

10 mod 7 = 3 → index 3rd is empty → insert 10.
17 mod 7 = 3 → collision → 3 + 1² = 4 → index 4th is empty → insert 17
24 → collision → 3 + 1² = 4 (full) → 3 + 2² = 7 mod 7 = 0 → index 0th is empty → insert 24

Now keys are more spread out.

Table looks like –

Quadratic Probing - Collision Handling using open addressing

Time Complexity – Quadratic Probing

  • Best case – O(1)
  • Average case – O(1) when α small
  • Worst case – O(n)

It reduces primary clustering but still has Secondary Clustering. Keys with same initial hash follow same probe sequence

Space Complexity – Quadratic Probing

The space complexity of Quadratic probing is O(m). There is no extra linked lists. It is only an array.

.

  1. Double Hashing

“In open addressing, when collision happens, we must search for another empty position inside the same hash table, we use one more hash function to decide how far we should jump.”

If collision happens at h1(k), then the next position is calculated as –

h1(k) + i × h2(k) (mod m) where i = 0 to m-1.

Here, h2(k) = k mod p, where p is equal to the prime number less than m.

.

So, h2(k) gives the jump value, and this jump value is different for different keys.

Because the step size depends on the key itself, keys do not follow the same probing pattern. So clustering is reduced a lot compared to linear and quadratic probing.

.

Algorithm – Double Hashing

Insertion –

  1. Compute h1(k)
  2. Compute h2(k)
  3. For i = 0 to m-1:
    index = (h1(k) + i × h2(k)) mod m
  4. Insert at first empty slot

Searching –

Follow same probing sequence.

  1. Compute index = h(k)
  2. If key found, return
  3. Else check next slot
  4. Stop when empty slot found or key located

Example

Let m = 7 and Hash function h1(k) = k mod 7
h2(k) = i + (k mod 5)
(i = 0 to m-1 & p = 5 less than m)

Insert key – 10, 17, 24

10 mod 7 = 3 → index 3rd is empty → insert 10.

17 mod 7 = 3 → collision → (h1(k) + (i*(h2(k)))) mod 7
This is equal to – (3 + (1*(17 mod 5))) mod 7 = (3 + 2) mod 7 = 5th index (empty) → insert 17.

24 mod 7 = 3 → collision → (h1(k) + (i*(h2(k)))) mod 7
This is equal to – (3 + (2*(17 mod 5))) mod 7 = (3 + 4) mod 7 = 0th index (empty) → insert 24.

Table looks like –

Quadratic Probing - Collision Handling using Open Addressing

Time Complexity – Double Hashing

  • Best case – O(1)
  • Average case – O(1)
  • Worst case – O(n)

Among all open addressing methods, double hashing gives best distribution.

Space Complexity – Double Hashing

The space complexity of Double Hashing is O(m). There is no extra linked lists. It is only an array.

.

Comparison

Linear Probing

  • Simple
  • Suffers from primary clustering

Quadratic Probing

  • Reduces primary clustering
  • Still secondary clustering

Double Hashing

  • Best distribution
  • Least clustering

But all require α < 1. As α approaches 1, performance collapses.

.

Applications of Open Addressing

Open Addressing is used in

  • Hash tables in embedded systems
  • Memory-constrained environments
  • Symbol tables in compilers
  • High-performance in-memory caches

Test Yourself

Q1- In Quadratic Probing, a table of size m may fail to insert even if empty slots exist because
  1. Load factor is zero
  2. Probe sequence does not cover all slots
  3. Hash function is perfect
  4. Table size is even

Ans – (2)

Explanation – Quadratic probing does not always guarantee visiting all table slots. Some empty slots may never be reached depending on m and hash function.

Q2- In Double Hashing, if h2(k) = 0 for some key, what happens?
  1. Perfect distribution
  2. Infinite loop
  3. Linear probing
  4. No collision

Ans – (2)

Explanation – If step size becomes zero, probe sequence becomes constant index. It will repeatedly check same slot and never move.

Q3- For unsuccessful search in Linear Probing, expected time increases rapidly when
  1. α is small
  2. α is close to 0.5
  3. α is close to 1
  4. m is prime

Ans – (3)

Explanation – As α, the load factor = n/m approaches 1, long clusters form. Unsuccessful search scans almost entire cluster.

Q4- If m = 10 and we use h(k) = k mod 10 with Linear Probing, inserting 20, 30, 40, 50 will create
  1. Even distribution
  2. No collision
  3. A growing cluster
  4. Secondary clustering

Ans – (3)

Explanation – All hash to 0. They will occupy 0, 1, 2, 3 forming a cluster.

Q5- If deletion marks slot as empty instead of “deleted”, then
  1. Performance improves
  2. Searching may fail incorrectly
  3. Clustering reduces
  4. Table resizes

Ans – (2)

Explanation – If we mark slot empty, search may stop early and conclude key not present, even if it exists later in probe sequence.

Q6- In Open Addressing, maximum number of elements stored is
  1. m
  2. n
  3. m-1 always
  4. Infinite

Ans – (1)

Explanation – Since all elements stored in array, capacity is m.

Q7- If table size is not prime in Double Hashing, possible issue is
  1. Faster insertion
  2. Infinite probing cycle
  3. Reduced memory
  4. No issue

Ans – (2)

Explanation – If step size and table size share common factors, probe may cycle over subset of indices only.

Q8- Which case gives worst clustering behavior?
  1. Linear Probing with α = 0.3
  2. Linear Probing with α = 0.95
  3. Double Hashing with α = 0.4
  4. Quadratic Probing with α = 0.2

Ans – (2)

Explanation – Linear probing at very high load factor causes huge primary clusters.

Q9- Give a scenario where Quadratic Probing fails even when table has empty slots.

Ans – If m is not chosen properly (for example, not prime), quadratic increments may repeat before covering all slots. Some empty slots remain unreachable.

Q10- Explain difference between primary and secondary clustering.

Ans – Primary clustering occurs when consecutive slots form large blocks due to linear probing. Secondary clustering occurs when keys with same initial hash follow same probe sequence.

Q11- Why do practical systems resize table around α ≈ 0.7?

Ans – Because after that point, expected probe length starts increasing rapidly. Resizing keeps performance near constant time.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha