Hashing in Data Structure: Hash Functions & Collision Handling

Data Structure / Chapter 40 / Collision Control Studio

Hashing in Data Structure: Hash Functions & Collision Handling

Why do different keys sometimes reach the same slot? Learn how a hash table works, calculate hash functions, resolve collisions and write complete C programs with clear diagrams, dry runs and an interactive simulator.

Hash Table Separate Chaining Linear & Quadratic Probing Double Hashing C Programming
Quick Answer / Definition

Hashing in Data Structure is a technique that uses a hash function to convert a key into an array index called a hash-table bucket or slot. A collision occurs when different keys map to the same index. Common collision handling methods are separate chaining and open addressing, including linear probing, quadratic probing and double hashing. Hash tables often provide expected O(1) insertion, search and deletion under suitable hashing and load conditions, but their worst-case cost can be O(n).

Imagine a college record system with hundreds of student IDs. Checking records one by one takes time. Instead, a hash function calculates where a record should be stored. When the function distributes keys well, the program can find the relevant bucket quickly. The challenge appears when two different IDs calculate the same slot.

This chapter explains hash tables, hash functions, modulus/division method, multiplication, mid-square and folding methods, collisions, separate chaining, linear probing, quadratic probing, double hashing, rehashing, load factor, deletion with tombstones, complete C programs, hashing time complexity and real applications . For basic algorithm terminology, see Algorithm in Data Structure .

Open the complete chapter roadmap
  1. Definition and terminology
  2. Hash functions and calculations
  3. What causes collisions?
  4. Separate chaining method
  5. Linear probing with dry run
  6. Quadratic and double hashing
  7. Insert, search, delete algorithms
  8. Interactive hashing simulator
  9. Chaining C program
  10. Linear probing C program
  11. Double hashing C program
  12. Load factor and rehashing
  13. Complexity comparison
  14. Applications and limitations
  15. Exam answers and MCQs
  16. FAQs and quick revision
01 / Learn the vocabulary

What Is Hashing and a Hash Table?

Hashing in Data Structure: Hash Functions & Collision Handling

A hash table stores items using positions calculated from their keys. For example, a student ID is the key and the student's record is its associated value. The hash function maps that key to a table index. A table can store keys alone (hash set) or key–value pairs (hash map).

Key

The identifier used to find an item, such as roll number 24 or a username.

Hash Function

A repeatable calculation h(key) that produces an index from 0 to table size − 1.

Bucket / Slot

The indexed storage location. In chaining, one bucket can reference multiple nodes.

Collision

Two different keys share the same bucket, such as 10 and 17 when using modulo 7.

Hashing is different from encryption. A classroom hash function is for locating records efficiently, not for keeping a password secret. Security-sensitive password storage uses dedicated, slow password-hashing algorithms with salts, not a simple hash-table modulus formula.

02 / Turn keys into indices

Types of Hash Functions in Data Structure

A good non-cryptographic hash function is deterministic: the same key maps to the same initial index for a given table size. It should be quick to calculate and aim to spread typical input keys reasonably evenly. It cannot promise a different index for every possible key when the number of keys exceeds the available slots.

1. Division / Modulus Method

Use h(k) = k mod m. For m = 7, h(24) = 24 mod 7 = 3. Often a prime-sized table is a helpful choice for basic examples, but input patterns also matter.

2. Multiplication Method

Use h(k) = floor[m × frac(kA)] with 0 < A < 1. Example: m = 10, k = 23 and A = 0.618 gives floor(10 × 0.214) = 2.

3. Mid-Square Method

Square the key and take a chosen set of middle digits/bits. For 23² = 529, one middle decimal digit is 2. The chosen digit/bit width depends on the table design.

4. Folding Method

Split a key into pieces and combine them. For 123456, groups 12 + 34 + 56 = 102. Using mod 10 afterward gives index 2.

Division method: h(k) = k mod m Multiplication method: h(k) = floor(m × frac(kA)) Hash table size: m = number of buckets Valid indices: 0 through m - 1

String hashing: Strings can be converted into integers through a rolling/polynomial hash, for example repeatedly updating h = (h × 31 + character_code) % m. Different strings can still collide, so a hash table must check that stored keys are actually equal.

C programming tip: C's % can produce a negative remainder for a negative key. For a positive m, normalise with ((key % m) + m) % m (or a conditional adjustment) to get a valid index.
03 / When routes overlap

What Is a Collision in Hashing? Why Does It Occur?

A hash collision occurs if two unequal keys produce the same initial hash index. With h(k) = k % 7, keys 10, 17, 24 and 31 all map to slot 3 because each leaves remainder 3.

h(10) = 10 % 7 = 3 h(17) = 17 % 7 = 3 h(24) = 24 % 7 = 3 h(31) = 31 % 7 = 3
0—
1—
2—
3 10 / 17 / 24 / 31
4—
5—
6—
All four keys want bucket 3. This is a conceptual collision before choosing how records are actually stored.

Collisions are unavoidable in general when infinitely many possible keys map into a finite number of buckets. A collision does not mean the hash function is broken. Instead, a correct hash table must use a collision resolution technique and compare actual keys during lookup.

04 / Collision method A

Separate Chaining in Hashing Using Linked Lists

Separate chaining, also called closed addressing in some textbooks, stores a collection of entries at every hash bucket. A common implementation uses a singly linked list per bucket. When keys collide, append or prepend a new node to that bucket's chain. Searches follow nodes and compare actual keys.

Bucket 0 NULL
Bucket 1 NULL
Bucket 2 NULL
Bucket 3 → 10 → 17 → 24 → 31 → NULL
Bucket 4–6 NULL
All four keys coexist in the linked-list chain of bucket 3.

Advantages: easy deletion once a node is located; the number of items can exceed the bucket count; no table-full probing failure while memory remains available. Limitations: extra node pointers and allocations, weaker memory locality, and longer chains when many keys collide.

For pointer and node basics, revise the verified Linked List in Data Structure chapter.

05 / Collision method B

Linear Probing in Hashing: Algorithm and Example

Linear probing is an open addressing technique. All items are stored inside the main table array. If the home slot is occupied by another key, check the next slot, wrapping around the end. A fixed table can become full.

Linear probe: index(i) = (h(k) + i) mod m For m = 7: i = 0, 1, 2, 3, ...
Insertion Home index Probe sequence checked Actual stored slot
10 3 3 3
17 3 3, 4 4
24 3 3, 4, 5 5
31 3 3, 4, 5, 6 6
0—
1—
2—
310
417
524
631
Linear probing places colliding keys in the next free slots. Consecutive occupied groups are called clusters.

Primary clustering: keys accumulate in continuous occupied runs, causing longer probes. Deletion warning: If slot 4 (17) is simply marked EMPTY, a later search for 24 may stop at slot 4 and incorrectly say not found. Use a DELETED marker (tombstone) or reorganise the affected cluster correctly.

06 / Advanced open addressing

Quadratic Probing and Double Hashing with Examples

Quadratic Probing

Use (h(k) + i²) mod m. With m = 7 and home 3, probe order starts 3, 4, 0, 5.... It reduces the primary clustering pattern of linear probing, but may not visit every slot. Insertion can fail even with empty positions, depending on capacity/load/probe rule.

Double Hashing

Use a second hash to choose the step: (h1(k) + i × h2(k)) mod m . Example with prime m = 7: h1(k) = k mod 7, h2(k) = 1 + (k mod 6) for non-negative keys. A non-zero step coprime to m can visit all slots.

Method Probe formula What changes? Important limitation
Linear (h + i) % m Step 1 Primary clustering
Quadratic (h + i²) % m Squared offsets May not reach all slots
Double hashing (h1 + i × h2) % m Second key-dependent step Step must be nonzero and coprime with m for a full-cycle probe

Mini dry run for double hashing: With m = 7, keys 10, 17, 24 have home index 3. Key 10 enters slot 3. Key 17 has step 6, so it probes 3 then 2. Key 24 has step 1, so it probes 3 then 4. Final positions: 10 → 3, 17 → 2, 24 → 4.

Exam memory trick: Linear = add 1; Quadratic = add i²; Double = use a second hash as the step. Separate chaining = follow nodes instead of searching other array slots.
07 / Insert, search and delete

Hash Table Operations and Collision Resolution Algorithms

Insert with Linear Probing

  1. Calculate home index h(key).
  2. Probe the home slot, then subsequent indices using modulo table size.
  3. If the key already exists, follow the update/duplicate policy.
  4. Record the first DELETED slot, but continue to check for an existing duplicate.
  5. Insert into the first reusable position, or report full if none exists.

Search with Linear Probing

  1. Start at h(key) and follow the same probe sequence used by insertion.
  2. If an OCCUPIED slot has the key, return its index.
  3. Skip DELETED markers; stop at a never-used EMPTY slot or after at most m probes.

Delete with Linear Probing

  1. Find the key using the same probe sequence.
  2. If present, set the slot's state to DELETED, not EMPTY.
  3. Future lookups continue across the tombstone; later inserts may reuse it.
Duplicate policy: The C examples in this chapter implement a hash set of integers: inserting an existing key does not create a second copy. Real hash maps may instead update the value associated with that key.
08 / Learn by experimenting

Interactive Hash Table Simulator: Chaining vs Linear Probing

Switch between separate chaining and linear probing. Enter an integer key and press Insert, Find or Delete. The default keys 10, 17 and 24 intentionally collide at home bucket 3. The table has seven slots. You can also try 31 or -4, which hashes to slot 3 after normalising the remainder.

Collision Control Desk

3 keys / 7 buckets

Ready. The keys 10, 17 and 24 are loaded.

Probe path: none yet.

Amber = visited slot, green outline = hash home, light green = matching result. DEL means a tombstone in linear probing. Chaining displays entries inside the same bucket.

Try deleting 17 in linear probing and then finding 24. The search must pass through the deleted slot to find 24. Change to chaining to see all colliding items together inside bucket 3. Switching the mode resets the demo so the two representations do not get mixed.

09 / Complete C program 1

Hashing in C Using Separate Chaining (Linked List)

This complete hash table C program using chaining supports insert, search, delete, display and memory cleanup. It stores unique integer keys and uses a linked-list chain in every bucket. It also supports negative keys safely.

hash_chaining.c C11 / Complete executable program
#include <stdio.h>
#include <stdlib.h>
#define SIZE 7

typedef struct Node {
    int key;
    struct Node *next;
} Node;

Node *bucket[SIZE] = {NULL};

int hash(int key) {
    int r = key % SIZE;
    return r < 0 ? r + SIZE : r;
}

int insert(int key) {
    int h = hash(key);
    Node **link = &bucket[h];
    while (*link != NULL) {
        if ((*link)->key == key) return 0;
        link = &(*link)->next;
    }
    Node *node = malloc(sizeof *node);
    if (node == NULL) return -1;
    node->key = key;
    node->next = NULL;
    *link = node;
    return 1;
}

int find(int key) {
    for (Node *p = bucket[hash(key)]; p; p = p->next)
        if (p->key == key) return 1;
    return 0;
}

int erase(int key) {
    Node **link = &bucket[hash(key)];
    while (*link != NULL) {
        if ((*link)->key == key) {
            Node *old = *link;
            *link = old->next;
            free(old);
            return 1;
        }
        link = &(*link)->next;
    }
    return 0;
}

void display(void) {
    for (int i = 0; i < SIZE; i++) {
        printf("Slot %d: ", i);
        for (Node *p = bucket[i]; p; p = p->next)
            printf("%d -> ", p->key);
        puts("NULL");
    }
}

void cleanup(void) {
    for (int i = 0; i < SIZE; i++) {
        Node *p = bucket[i];
        while (p != NULL) {
            Node *next = p->next;
            free(p);
            p = next;
        }
        bucket[i] = NULL;
    }
}

int main(void) {
    insert(10); insert(17); insert(24); insert(-4);
    display();
    printf("Find 17: %s\n", find(17) ? "Found" : "Missing");
    printf("Delete 17: %s\n", erase(17) ? "Done" : "Missing");
    display();
    cleanup();
    return 0;
}
Slot 0: NULL Slot 1: NULL Slot 2: NULL Slot 3: 10 -> 17 -> 24 -> -4 -> NULL Slot 4: NULL Slot 5: NULL Slot 6: NULL Find 17: Found Delete 17: Done Slot 0: NULL Slot 1: NULL Slot 2: NULL Slot 3: 10 -> 24 -> -4 -> NULL Slot 4: NULL Slot 5: NULL Slot 6: NULL

Working: All four keys belong to bucket 3. The insert() function checks for duplicates and appends a new node if memory allocation succeeds. erase() uses a pointer-to-pointer so it can remove even the first node correctly. cleanup() frees all remaining nodes. The programme distinguishes a duplicate insertion (return 0) and allocation failure (return -1).

10 / Complete C program 2

Linear Probing in C with Search, Deletion and Tombstones

This linear probing C program implements a fixed array of seven slots. Each slot is EMPTY, OCCUPIED or DELETED. Deletion leaves a tombstone so future lookups work even when several keys collided earlier.

hash_linear_probing.c C11 / Complete executable program
#include <stdio.h>
#define SIZE 7

typedef enum { EMPTY, OCCUPIED, DELETED } State;
typedef struct { int key; State state; } Slot;
Slot table[SIZE];

int hash(int key) {
    int r = key % SIZE;
    return r < 0 ? r + SIZE : r;
}

int find(int key) {
    int home = hash(key);
    for (int j = 0; j < SIZE; j++) {
        int i = (home + j) % SIZE;
        if (table[i].state == EMPTY) return -1;
        if (table[i].state == OCCUPIED &&
            table[i].key == key) return i;
    }
    return -1;
}

/* 1 = inserted, 0 = duplicate, -1 = full */
int insert(int key) {
    int home = hash(key);
    int deleted = -1;
    for (int j = 0; j < SIZE; j++) {
        int i = (home + j) % SIZE;
        if (table[i].state == OCCUPIED) {
            if (table[i].key == key) return 0;
        } else if (table[i].state == DELETED) {
            if (deleted == -1) deleted = i;
        } else {
            int dest = deleted == -1 ? i : deleted;
            table[dest].key = key;
            table[dest].state = OCCUPIED;
            return 1;
        }
    }
    if (deleted == -1) return -1;
    table[deleted].key = key;
    table[deleted].state = OCCUPIED;
    return 1;
}

int erase(int key) {
    int i = find(key);
    if (i == -1) return 0;
    table[i].state = DELETED;
    return 1;
}

void display(void) {
    for (int i = 0; i < SIZE; i++) {
        if (table[i].state == EMPTY)
            printf("%d: EMPTY\n", i);
        else if (table[i].state == DELETED)
            printf("%d: DELETED\n", i);
        else
            printf("%d: %d\n", i, table[i].key);
    }
}

int main(void) {
    insert(10); insert(17); insert(24);
    puts("Before deletion:"); display();
    erase(17);
    printf("Find 24 after deleting 17: %d\n", find(24));
    insert(31);
    puts("After inserting 31:"); display();
    return 0;
}
Before deletion: 0: EMPTY 1: EMPTY 2: EMPTY 3: 10 4: 17 5: 24 6: EMPTY Find 24 after deleting 17: 5 After inserting 31: 0: EMPTY 1: EMPTY 2: EMPTY 3: 10 4: 31 5: 24 6: EMPTY

Observe: Slot 4 becomes DELETED after removing 17. The search for 24 skips that tombstone and reaches slot 5. Inserting 31 reuses slot 4 only after checking that 31 does not already exist further along its probe path. Duplicate handling must not insert a second copy just because a tombstone appears first.

11 / Complete C program 3

Double Hashing Implementation in C

The following programme demonstrates double hashing in C using m = 7 (prime). h1(key) chooses the home slot and h2(key) chooses a non-zero step from 1 to 6. Because 7 is prime, these steps are coprime to 7 and the probe can visit every slot before repeating.

hash_double_hashing.c C11 / Complete executable program
#include <stdio.h>
#define SIZE 7

typedef struct { int key, used; } Slot;
Slot table[SIZE];

int mod(int key, int size) {
    int r = key % size;
    return r < 0 ? r + size : r;
}

int h1(int key) { return mod(key, SIZE); }
int h2(int key) { return 1 + mod(key, SIZE - 1); }

int insert(int key) {
    int home = h1(key), step = h2(key);
    for (int j = 0; j < SIZE; j++) {
        int i = (home + j * step) % SIZE;
        if (table[i].used && table[i].key == key)
            return 0; /* Duplicate */
        if (!table[i].used) {
            table[i].used = 1;
            table[i].key = key;
            return 1;
        }
    }
    return -1; /* Full */
}

int find(int key) {
    int home = h1(key), step = h2(key);
    for (int j = 0; j < SIZE; j++) {
        int i = (home + j * step) % SIZE;
        if (!table[i].used) return -1;
        if (table[i].key == key) return i;
    }
    return -1;
}

int main(void) {
    insert(10); insert(17); insert(24);
    for (int i = 0; i < SIZE; i++) {
        if (table[i].used) printf("%d: %d\n", i, table[i].key);
        else printf("%d: EMPTY\n", i);
    }
    printf("Find 17 at slot %d\n", find(17));
    return 0;
}
0: EMPTY 1: EMPTY 2: 17 3: 10 4: 24 5: EMPTY 6: EMPTY Find 17 at slot 2

Note: This short double-hashing example supports insertion and lookup but not deletion. A production version must implement deletion safely (usually with tombstones), handle growth and choose its table-size/step design carefully. For the full deletion logic, study the linear-probing program above.

How to run these C programs: In VS Code, save them as hash_chaining.c, hash_linear_probing.c and hash_double_hashing.c. Compile separately with gcc -std=c11 -Wall -Wextra hash_chaining.c -o hash_chaining (replace filenames for the others), then run hash_chaining.exe on Windows or ./hash_chaining on Linux/macOS. Each source file has its own main().

12 / Keep hash tables efficient

Load Factor, Rehashing and Resizing in Hash Tables

The load factor is usually written as the Greek letter alpha (α). It measures the average number of stored entries per bucket for separate chaining, or the fraction of occupied slots for open addressing.

Load factor: α = n / m n = stored keys, m = buckets/slots Example: n = 5, m = 7 → α ≈ 0.714

Separate Chaining

Load factor may be greater than 1, because a bucket can contain several nodes. Larger α usually means longer average chains, assuming a suitable hash distribution.

Open Addressing

All keys occupy slots in the array; the occupied-slot load factor cannot exceed 1. Performance generally worsens as α approaches 1. Tombstones may also increase probing work.

Rehashing means allocating a new table (often larger) and inserting existing keys again using the new capacity and its hash function. Merely copying keys to the same old indices is not correct when capacity changes, because key % m then produces different indices. Rebuilding costs O(n) for that resize operation under ordinary assumptions, even when individual inserts are expected O(1).

Typical engineering guidance: implementers may grow an open-addressed table around a chosen load threshold such as 0.7, but there is no universal threshold for every probing method. Quadratic probing can require stricter conditions; monitor both occupied slots and tombstones.

13 / Analyse expected and worst cases

Hashing Time and Space Complexity Comparison

Let n be stored keys and m be buckets. Expected/average performance below assumes a reasonable hash distribution, suitable load factor and constant-time key comparisons. Poorly distributed or adversarial keys can cause much slower operations.

Technique Insert / Search / Delete expected Worst case Storage
Separate chaining O(1 + α) (often O(1) with bounded α) O(n) chain O(m + n)
Linear probing Expected O(1) when load controlled O(m) probes O(m)
Quadratic probing Expected O(1) with suitable conditions O(m) probes / may fail before full O(m)
Double hashing Expected O(1) when load controlled O(m) probes O(m)
Rehash/resizing Not an ordinary single lookup O(n + new capacity) for rebuild New table allocation

Best case: O(1) for a home-slot hit or an empty bucket. Expected case: commonly O(1) when hashing and resizing keep collision costs small. Worst case: O(n) for a long linked chain or O(m) probing a fixed table. When m is proportional to n, both worst-case descriptions are commonly presented as O(n).

Exam caution: Saying “hashing always takes O(1)” is incorrect. O(1) is typically an expected/amortized statement under conditions, not a strict worst-case guarantee.
14 / Applications and practical decisions

Applications, Advantages and Disadvantages of Hashing

Applications

  • Dictionary/hash map lookups by key
  • Checking membership in a hash set
  • Symbol tables in compilers/interpreters
  • Caches and in-memory indexes
  • Counting frequencies and grouping duplicate values
  • Deduplication and some join algorithms

Advantages

  • Fast expected insert/find/delete
  • Useful when exact-key lookup matters
  • Works without sorting records first
  • Can support flexible key–value mappings
  • Many mature library implementations exist

Disadvantages

  • Collisions require extra logic
  • Worst-case performance may be linear
  • Memory used by empty buckets or pointers
  • Maintaining load factor may trigger resizing
  • Not naturally sorted for range queries

Hashing vs Searching

Linear search may inspect up to n entries. Binary search uses sorted indexed data and O(log n) worst-case time. A hash table often offers expected O(1) exact-key lookup but needs storage, hashing and collision handling; it does not normally maintain sorted order.

Security distinction: For user-controlled keys, weak hashing can create many collisions and become a denial-of-service problem. Some implementations use randomized hashing or other protective strategies. Hash-table hashing is not a replacement for password-hashing functions.

15 / University exam preparation

Hashing Important Questions with Answers (2, 5 and 10 Marks)

These are original exam-style/PYQ-style practice questions, not verified past-year questions from a named university.

2 Marks

Q1. Define hashing.

Hashing maps a key to a hash-table index using a hash function to support efficient storage and retrieval.

Q2. What is a hash collision?

A collision occurs when two different keys produce the same initial hash index.

Q3. Define load factor.

Load factor α = n/m, where n is stored entries and m is number of buckets.

Q4. Name two collision resolution techniques.

Separate chaining and open addressing (such as linear probing or double hashing).

5 Marks

Q5. Explain linear probing using h(k) = k % 7.

Insert 10, 17, 24 and 31. All initially hash to index 3. Insert 10 at 3; when index 3 is occupied, 17 goes to 4, 24 to 5, and 31 to 6. This illustrates step-by-step probing and clustering. A search must follow the same path.

Q6. Differentiate chaining and open addressing.

In chaining, each bucket points to a linked sequence of colliding entries and more than m items can be stored. In open addressing, entries stay in the fixed table itself and alternative slots are checked. Chaining has pointer/allocation overhead; probing can suffer clustering and requires careful tombstone deletion.

Q7. Compare linear, quadratic and double hashing.

Linear probes by +i, quadratic by +i² and double hashing by i times a second hash step. Linear probing suffers primary clustering, quadratic probing may not visit every slot, and double hashing benefits from a key-dependent step that should be coprime to table size.

10 Marks

Q8. Explain hashing, hash functions, collision handling, C implementation and complexity.

Start with the definition and h(k) = k % m. Show a table of 7 buckets and a collision example using keys 10, 17 and 24. Explain four types of hash functions. Draw separate chaining and linear probing diagrams, then describe quadratic probing and double hashing. Write a complete C implementation from Sections 9 or 10. Finish by explaining load factor, rehashing, expected O(1) operations under good conditions, worst-case linear behaviour and applications of hashing.

Q9. Explain why tombstones are necessary in linear probing.

Insert keys 10, 17, 24 into slots 3, 4 and 5. If 17 is deleted and index 4 is marked never-used EMPTY, a search for 24 can stop too early at 4. Marking slot 4 DELETED tells lookup to continue, so key 24 can still be found at slot 5. Insertion can later reuse the tombstone after checking the probe path for duplicates.

Multiple-Choice Questions with Answers

  1. h(24) for m = 7? A. 1 B. 2 C. 3 D. 4. Answer: C, since 24 % 7 = 3.
  2. Collision means: A. Empty table B. Same index for unequal keys C. Sorted array D. Deleted key. Answer: B.
  3. Linear probing formula: A. h+i B. h×i C. h−i² D. n/i. Answer: A (modulo m).
  4. Which method stores linked nodes per bucket? A. Chaining B. Binary search C. Bubble sort D. Stack. Answer: A.
  5. Which method uses i² offsets? A. Linear B. Quadratic C. Chaining D. Folding. Answer: B.
  6. What is α when n = 5, m = 10? A. 5 B. 2 C. 0.5 D. 10. Answer: C.
  7. Tombstone in open addressing means: A. Never used B. Previously occupied but deleted C. Hash function D. Overwritten array. Answer: B.
  8. Hashing worst-case search may take: A. Always O(1) B. O(n) C. Always O(log n) D. O(0). Answer: B.
16 / Quick revision

Hashing FAQs and Last-Minute Revision Notes

What is hashing in data structure in simple words?

Hashing calculates where an item should be stored by using its key. The calculated index points to a bucket or slot in a hash table.

What is the difference between hash function and hash table?

A hash function calculates an initial index from a key. A hash table is the storage structure that uses that index and handles possible collisions.

Why do collisions occur in hashing?

Different possible keys can map to the same one of a finite number of buckets. Collision resolution is therefore part of a correct hash table design.

Which is better, chaining or linear probing?

It depends. Chaining makes deletion straightforward and permits more entries than buckets, but uses linked-node memory. Linear probing stores records compactly but can cluster and needs tombstone-aware deletion.

What is the difference between linear and quadratic probing?

Linear probing checks slots at offsets 0,1,2,3...; quadratic probing uses offsets 0,1,4,9... modulo capacity. Quadratic probing's probe sequence may not reach all positions.

What is double hashing?

It is an open addressing technique that uses a second hash function as the probe step. A non-zero step coprime to table size allows a full cycle of candidate slots.

What does rehashing mean?

Rehashing rebuilds entries in a table, often after increasing capacity or to clear tombstones. Every stored key must be placed according to the new table configuration.

Is hashing always O(1)?

No. O(1) is normally an expected lookup/insertion/deletion cost with suitable load and hash distribution. Poor collision patterns can make operations O(n) or O(m) in a fixed table.

Is hash table hashing the same as password hashing?

No. Hash tables optimise storage and exact-key lookup. Password hashing uses dedicated password-hashing algorithms designed to resist attacks.

Exam memory map

Key → Hash function → Home slot → Collision? → Resolve → Insert/Search/Delete.

Must-know formulas

h(k) = k mod m; α = n/m; linear: (h+i)%m; quadratic: (h+i²)%m; double: (h1+i×h2)%m.

Data Structure / Chapter 40 / Complete

Conclusion: Same Hash Slot, Different Collision Solutions

Hashing makes exact-key lookup efficient by computing a likely storage location. The key lesson is that two keys can calculate the same index, so correct collision handling is essential. Separate chaining creates a list of entries in one bucket; probing searches alternative table slots. Load factor, good key distribution and careful deletion determine how well a hash table performs.

For university exams, practise modulus calculations, collision dry runs, all three probing formulas, load-factor examples and the complete C programs . Always distinguish expected O(1) performance from the worst case.

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Join Telegram Exam updates and free resources