What is the complexity of accessing an element in a hash table?
The average time complexity of accessing (looking up) an element in a hash table is O(1), that is, constant time.
Why O(1):
- The hash function computes the index in fixed time.
- The table accesses the right slot (bucket) directly.
- On average, a bucket holds 1 element or very few.
In the worst case: If all elements produced the same hash (maximum collisions), they all end up in one bucket, and the lookup turns into O(n), the entire chain has to be walked.
Summary:
- Average complexity: O(1)
- Worst-case complexity: O(n)
- Amortized (in practice): O(1) with a good hash function and a balanced table.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.