Data Structures19 sections · 729 units
Open in Course

Hash Table Operations

Average vs worst case

Average case: O(1)O(1) for insert, lookup, delete. Worst case: O(n)O(n) if all keys collide (hash to the same index).

Collisions are rare with good hash functions and proper table sizing. In competitive programming, assume O(1)O(1) unless you're dealing with adversarial inputs.

Some judges craft test cases to break weak hash functions. If you get TLE, consider using a tree-based map (O(logn)O(\log n)) instead. Time: O(1)O(1). Space: O(n)O(n).