The Runtime Theory
RuntimeInternalsmemory

Trace a Hash Table Lookup

Follow a key through hashing, bucket selection, collision handling, equality checks, and the returned value.

The Runtime Theory Team1 min read04 steps

layer stack

Runtime

HWHardware
KKernel
RTRuntime
APPApplication
SYSSystem
CLIClient
NETNetwork
TLSCrypto
SRVServer

trace spine

  1. 01 Compute a hash from the lookup key
  2. 02 Map the hash to a table position
  3. 03 Inspect the bucket or probe sequence
  4. 04 Compare stored keys and return the match
▸ On this page

Assume a map stores "pear" → 7 and a caller asks for get("pear"). The exact operations depend on the language and implementation, but a hash table must turn the key into a candidate location and still verify that the stored key is equal.

1. Hash the key

The implementation calls the key's hash function and may mix the result so its bits distribute well across the table. The hash is not the key and collisions are possible: distinct keys can produce the same hash.

2. Select a candidate bucket

The table converts the hash to a bucket index using its capacity and representation. Some tables use a mask when capacity is a power of two; others use a remainder or another scheme. Resizing changes the mapping and may require reinserting entries.

3. Resolve collisions

With chaining, the bucket can contain multiple entries. With open addressing, the implementation probes additional slots according to a policy. In either case, the lookup may inspect more than one candidate. A deleted entry in an open-addressed table can require a tombstone so later keys remain discoverable.

4. Check equality, then return

The map compares candidate keys using the language's equality rules. Matching hash alone is insufficient. On equality it returns the associated value; after exhausting valid candidates it reports absence. Expected constant-time behavior depends on a reasonable hash distribution and controlled load factor, not a guarantee that every lookup performs one memory access.

Not started

Sign in to save your learning progress.

Sign in to save