Skip to contentKEEP THE CRAFTTHE SANCTUARY

Find a Key in Hash Buckets

Look up a key in a hand-built hash table: hash to one bucket, then compare exact keys.

The problem

Implement resolve_hash_bucket(buckets, key). buckets is a hash table built by hand: a list of buckets, where each bucket is a list of [stored_key, value] pairs. A key is always stored in bucket key % len(buckets), and different keys can land in the same bucket. Return the value stored for key, or None if key is not in the table. For example, with buckets = [[], [[1, "one"], [5, "five"]], [], []] and key = 5, 5 % 4 is 1, so you look in bucket 1 and return "five". Keys are integers and may be negative; there is always at least one bucket.

Starter code

def resolve_hash_bucket(buckets, key):
    pass

Public examples

  1. buckets = [[], [[1, "one"], [5, "five"]], [], []], key = 5gives"five"
  2. buckets = [[], [[1, "one"]], [], []], key = 9givesNone

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.