StudyGraph
Computer Science Explainer

When O(1) hashing collapses to O(n)

Your hash table is O(1)... until a bad hash function makes it O(n).

Lesson notes

Key points

Definitions

Simple uniform hashing
Assumption that each key is equally likely to land in any of the m slots, independently.

Takeaway: Hashing is O(1) only on average; a bad hash function makes it O(n).

Quick check: Worst-case lookup with chaining when all keys collide?
  1. O(1)
  2. O(log n)
  3. O(n)

All n keys sit in one chain, so a lookup may scan all of them.

Uniform hashingWorst case O(n)Clustered keysGood hash design

Lessons like this, for your syllabus.

Short narrated study videos written for your course, level and exam. Free to start.

Get lessons for your own syllabus