Skip to main content

What is a "bucket" in the context of a hash table?

A bucket is a slot in a hash table where elements whose keys hashed to the same index are stored.

How it works

  1. The hash function computes the index for a key:
javascript
index = hash(key) % N
  1. All elements for which this index matches land in the same bucket.
  2. If there are several such elements, this is a collision, and the bucket stores all of them (for example, as a list).

Example

If hash("cat") % 10 = 3 and hash("dog") % 10 = 3, both elements will sit in the bucket with index 3.

javascript
bucket[3][("cat", "meow"), ("dog", "woof")]

Purpose

Buckets exist to resolve collisions: to hold multiple elements that landed in the same slot.

In other words, a bucket is a mini storage inside a hash table, which can hold one or several "key -> value" pairs with the same hash index.

Short Answer

Interview ready
Premium

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