Skip to main content

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):

  1. The hash function computes the index in fixed time.
  2. The table accesses the right slot (bucket) directly.
  3. 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.