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.

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?

In interviews the two terms are used interchangeably: both store key-value pairs and find keys with a hash function. Some languages have separate classes with both names (Java has Hashtable and HashMap, for example), but the underlying data structure is the same idea.

What is the time complexity of a hash table lookup?

O(1) on average for lookups, inserts and deletes. In the worst case, when many keys collide in the same slot, an operation can take O(n) time, but a good hash function and resizing the table as it fills up make that rare.

When should I use a hash map in a coding interview?

Whenever your solution searches a list for an item over and over: to count occurrences, to check whether you've seen something before, or to find two items that add up to a target. A hash map trades O(n) extra space for fast lookups, which often turns an O(n²) solution into an O(n) one.

What is a hash collision?

A collision happens when two different keys hash to the same slot in the underlying array. Hash tables handle collisions either by chaining, where each slot holds a list of entries, or by open addressing, where the table probes for another empty slot.

Can I use a hash set instead of a hash map?

Yes, when you only need to know whether an item is present and don't need to store a value with it, as when checking for duplicates. A hash set gives the same O(1) average-time lookups as a hash map.
. . .