Hash Table Interview Questions
How to approach hash table questions
Hash tables (called hash maps, dictionaries or objects, depending on the language) are one of the most useful tools for speeding up a brute-force solution. They insert and look up a key in O(1) time on average, so a nested loop that compares every pair of items can often become a single pass that checks a hash map for what it needs.
The typical patterns: count how many times each item appears, remember the items you've already seen so you can spot a duplicate or a matching pair, and group items by a computed key. The trade-off is space. A hash map usually costs O(n) extra memory, and a common follow-up is to ask whether you can do without it.
Know what's going on underneath, too. A hash function turns each key into an index in an array, and when two keys land in the same slot (a collision), the table has to handle it. That's why lookups are O(1) on average but O(n) in the worst case.
Interview Cake isn't just another question database. Each question below walks you through the problem step by step, with hints and explanations as you need them, just like a real interviewer.
Hash table practice questions
Given a flight length and a list of movie lengths, find two movies that add up to exactly the flight length: a real-world two sum problem, in O(n) time. keep reading »
Permutation Palindrome »Check if any permutation of an input string is a palindrome. keep reading »
Word Cloud Data »You're building a word cloud. Write a function to figure out how many times each word appears so we know how big to make each word in the cloud. keep reading »
Find Duplicate Files »Your friend copied a bunch of your files and put them in random places around your hard drive. Write a function to undo the damage. keep reading »
Cake Photo Cache »Build a fixed-size photo cache that evicts the least recently used photo, with O(1) get and put: the LRU cache interview question, step by step. keep reading »
Keep going with the Hashing and hash tables section of our coding interview course.
Concepts to review
- Hash tables: How hash tables store key-value pairs and handle collisions.
- Hashing and hash functions: What a hash function does, and what makes a good one.
- Arrays: The array that sits underneath every hash table.
- LRU cache: A popular design question that pairs a hash map with a doubly linked list.
- Bloom filters: A space-efficient way to test set membership with several hash functions, at the cost of occasional false positives.
Quick reference: Data structures cheat sheet
Hash table interview FAQ
What's the difference between a hash table and a hash map?
What is the time complexity of a hash table lookup?
When should I use a hash map in a coding interview?
What is a hash collision?
Can I use a hash set instead of a hash map?
More interview questions by topic
- Array and string interview questions
- Greedy algorithm interview questions
- Sorting and binary search interview questions
- Binary tree and BST interview questions
- Graph interview questions
- Dynamic programming and recursion interview questions
- Stack and queue interview questions
- Linked list interview questions
- Bit manipulation interview questions
- System design interview questions
- All coding interview questions
Interview Cake