LearnThatStack Ace your next interview
Arrays, Strings & Hash Tables · question
Question 6 of 85

What is a hash table and what problem does it solve?

beginner
← All Arrays, Strings & Hash Tables questions
Re-explain
Visual ↓

A hash table stores key-value pairs and lets you insert, look up or delete by key in O(1) average time. The problem it solves is slow lookup. Searching an unsorted array means checking every element, which is O(n).

An array finds an element in one step only when you know its index. A hash table computes that index from the key. A hash function turns the key into a bucket number, and a lookup recomputes it to jump straight there.

Python's dict and Java's HashMap are both hash tables.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Arrays, Strings & Hash Tables One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
How a hash table finds a key without scanning
Unsorted arraychecked one by one
i
catbeefoxdogantowl
1 check
Hash functionkey in, bucket number out
keyowl
hash?
bucket?
Bucket arrayindex computed from the key
bee-catowl-antfoxdog
1step

We want the key owl. An unsorted array gives no clue where it sits, so the search has to start at the first element.

1 / 6

A hash function turns the key into a bucket number, so a lookup jumps to one bucket instead of checking every element.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

What you can say

  1. A hash table stores key-value pairs and lets you insert, look up or delete by key in O(1) time on average.
  2. The problem it solves is slow lookup, because finding a value in an unsorted array means checking every element, which is O(n).
  3. An array finds something in one step only when you already know its index, and a hash table works out that index from the key.
  4. A hash function turns the key into a bucket number, and a lookup runs the same function again to jump straight to that bucket.
  5. You're using one every time you reach for a Python dict or a Java HashMap.
  6. The catch is that O(1) is only the average, because keys can collide in one bucket, and the table keeps no sorted order, so it can't answer range queries.

Weak answers to avoid

  • Says O(1) without the word 'average' Collisions can slow lookups, up to O(n) in the worst case. Say O(1) on average and name collisions as the reason.
  • Defines it but skips the problem it solves The question asks what it solves. Contrast it with an O(n) scan of an unsorted array, then show how computing the index from the key avoids that scan.
  • Treats the hash function as magic 'It hashes the key' explains nothing. Say the function turns the key into a bucket number, and a lookup recomputes it to jump there.
  • Implies the keys come back sorted A hash table places keys by their hash value, so there is no sorted order. If you need sorted keys or ranges, say a tree map fits better.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Arrays, Strings & Hash Tables cheatsheet.

← Back to all Arrays, Strings & Hash Tables questions
Pro · $10/mo

74 of 85 Arrays, Strings & Hash Tables answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime