Data Structures · Visualization

Hash Table: Get

Hash Table

Getting a value looks up the bucket for a given key using the same hash function used during insertion, then scans that (usually very small) bucket for the matching key. Time: O(1) average, O(n) worst case. Space: O(1).

Big-O: Average O(1) per operation; worst case O(n) on collisions.

Hash Table: Get

Hash Table

Getting a value looks up the bucket for a given key using the same hash function used during insertion, then scans that (usually very small) bucket for the matching key. Because the hash function narrows the search down to essentially one bucket, this is extremely fast on average.

How Get Works

  1. Compute the hash of the key and convert it to a bucket index.
  2. Go directly to that bucket.
  3. Scan the (typically short) list of entries in the bucket for a matching key.
  4. Return the associated value if found, or 'not found' otherwise.

Get Worked Example

Input: Get value for key 'name'

  1. hash('name') % size = 3
  2. Look in bucket 3
  3. Find entry ('name', 'Sam') — match!

Output: 'Sam'

Try your own input
Hash Table: Introduction
Runs each key through a hash function to compute a bucket index, so lookups don’t need to scan every entry.
0
∅
1
∅
2
∅
3
∅
4
∅
5
∅
6
∅
Active Hash map Key
Hash Table: Introduction
Press ▶ Run to watch it step by step.
Python · Hash Tables in Code
# Hash map: key -> value, O(1) average
scores = {}
scores["alice"] = 90     # insert
v = scores.get("bob", 0) # get with default
del scores["alice"]      # delete

if "bob" in scores:      # membership
    print("present")
# example: get key=name
Input: Get value for key 'name'

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Get Time & Space Complexity

TimeO(1) average, O(n) worst case
SpaceO(1)

When to Use Get

  • Looking up a user's data by their ID
  • Checking if a cached result already exists before recomputing it
  • Fast dictionary/vocabulary lookups

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.