FirstHack Learn
Log in Sign up free
Lessons in this course 0/6 All courses Data Structures in C

CSE

Progress0 / 6 lessons
  1. 1. What a data structure buys you
  2. 2. Arrays vs linked lists
  3. 3. A singly linked list from scratch
  4. 4. Stacks and queues
  5. 5. Binary search trees
  6. 6. Hash tables and collisions

Courses › Data Structures in C

Hash tables and collisions

O(1) lookup, how the hash function decides everything, and what to do when two keys collide.

13 min read · Lesson 6 of 6 · Pro

Skipping the search entirely

Every structure so far searches: a list walks, a tree compares. A hash table does neither. It computes where the item should be.

C
int table[10];
int key = 47;
int index = key % 10;      /* 7 */
table[index] = key;

Storing and finding are both one arithmetic operation. That is O(1), and it does not grow with n. Nothing else on your syllabus does this.

The catch

47 % 10 is 7. So is 37 % 10, and 107 % 10. Three different keys, one slot. That is a collision, and it is not a rare accident you can design away.

The rest of this lesson is Pro

The free lessons of Data Structures in C finish what they start — read those first if you have not. This one goes further, and it is part of the paid half.

A pass opens the paid lessons of every course, the mock test papers and the company-wise series. It ends on its own date; nothing renews by itself.

Get a pass — from ₹29 for 7 days

Already bought one? Log in.