Skip to content
Coding Interviews

Hash Tables and Hash Maps Explained

By The EbookWale Team · Updated June 16, 2026 · 5 min read

A hash table stores key-value pairs and uses a hash function to find them in O(1) average time. Here's how hashing works, how collisions are handled, and why hash maps are the most useful coding-interview tool.

A hash table stores key-value pairs and uses a hash function to find any entry in O(1) average time. It’s the structure behind Python’s dict, JavaScript’s Map/objects, and Java’s HashMap — and it is, by a wide margin, the most useful tool in coding interviews.

The reason it matters so much: a huge number of “brute force is too slow” problems are solved by the same move — store what you’ve seen in a hash map so you can look it up instantly instead of searching again.

How hashing works

The magic is the hash function: it converts a key into a number, which maps to an index (a bucket) in an internal array.

key "Ana"  --hash-->  3491  --% buckets-->  index 7  --> store value here

To look “Ana” up later, you hash it again, jump straight to index 7, and there’s the value — no scanning. That direct jump is why lookup is O(1) on average, regardless of how many entries there are.

"Ana" key hash() Ana → 30 0 1 2 3 buckets
A hash function maps a key straight to its bucket — O(1).
const ages = new Map();
ages.set("Ana", 30);     // hash "Ana" → bucket → store
ages.get("Ana");         // hash "Ana" → bucket → 30, O(1)
ages.has("Ben");         // O(1) membership test

Collisions — when two keys land in the same bucket

Different keys can hash to the same index. That’s a collision, and there are two standard fixes:

  • Chaining — each bucket holds a short list; collided entries sit in the same list and are scanned (a tiny list, so still fast).
  • Open addressing — if a bucket is taken, probe forward to the next free slot.

When a table gets too full, it resizes (rehashes everything into a bigger array) to keep buckets short. This is why the worst case is O(n) — many collisions in one bucket — but the average stays O(1).

The Big O reality

OperationAverageWorst case
InsertO(1)O(n)
LookupO(1)O(n)
DeleteO(1)O(n)

Worst case only happens with a pathological hash function or adversarial keys; in normal use, treat hash maps as O(1). See Big O notation for why the average is what matters at scale.

The interview superpower: trading space for time

Here’s the move that wins interviews. The classic “two-sum” — find two numbers that add to a target:

// Brute force: check every pair → O(n²)
for (let i = 0; i < nums.length; i++)
  for (let j = i + 1; j < nums.length; j++)
    if (nums[i] + nums[j] === target) return [i, j];

// Hash map: remember what you've seen → O(n)
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
  const need = target - nums[i];
  if (seen.has(need)) return [seen.get(need), i];
  seen.set(nums[i], i);
}

The hash-map version does one pass, checking “have I already seen the number I need?” in O(1). That single idea — store seen values to avoid re-searching — converts dozens of O(n²) problems to O(n).

🔑 REMEMBER — When a problem says "find a pair", "count occurrences", "detect duplicates", "group by", or "have I seen this?", reach for a hash map first. Spending O(n) memory to save O(n) time is the most common — and most rewarded — interview optimisation.

The patterns hash maps unlock

  • Counting / frequency — tally occurrences (anagram checks, “most frequent” problems).
  • Grouping — bucket items by a key (group anagrams, group by category).
  • Deduplication & membership — a hash set for “seen before” in O(1).
  • Caching / memoization — store computed results, the basis of dynamic programming.

Common mistakes

  • Mutable keys. In most languages a key must be immutable/hashable — a list can’t be a key, a tuple can. Mutating a key after insertion corrupts lookups.
  • equals/hashCode in Java. Custom objects used as keys must override both, or “equal” objects won’t be found (see the Java Collections framework).
  • Assuming order. A plain hash map isn’t sorted; some preserve insertion order (Python dicts, JS Maps), but never rely on sort order.
  • Forgetting worst case in adversarial settings — usually irrelevant, occasionally a discussion point.
⚠️ GOTCHA — Calling list.includes(x) or list.indexOf(x) inside a loop is a hidden O(n²). Load the values into a hash set first, then each membership check is O(1) and the whole thing drops to O(n).

Where this fits

Hash tables are Phase 2 of the coding interview roadmap and the most important entry in the data structures overview — the tool you’ll reach for in more problems than any other.

How hashing works and the space-for-time patterns are drawn out in our job-ready tier — JavaScript in Three Months, Python in Three Months, and Java in Three Months. For hash-table internals — collision strategies, load factors, and building one from scratch — the for Staff Engineers tier goes deeper: JavaScript, Python, Java.

When in doubt in an interview, ask yourself: “would a hash map make this O(n)?” The answer is yes more often than you’d think.

Frequently asked questions

What is a hash table?

A hash table is a data structure that stores key-value pairs and uses a hash function to convert each key into an array index, giving near-instant O(1) average lookup, insertion, and deletion. It is called a dict in Python, an object or Map in JavaScript, and a HashMap in Java.

How does a hash function work?

A hash function takes a key and returns a number (the hash), which is mapped to an index in the underlying array (a bucket). The value is stored at that index, so looking it up later means re-hashing the key to jump straight to the right bucket — no scanning required.

What is a hash collision and how is it handled?

A collision happens when two different keys hash to the same bucket. The two common fixes are chaining (each bucket holds a small list of entries) and open addressing (probe to the next free slot). Good hash functions and resizing keep collisions rare, preserving O(1) average performance.

Why are hash maps so useful in coding interviews?

Because they turn repeated searching into instant lookup. Many brute-force solutions are O(n²) because they search a list inside a loop; storing seen values in a hash map makes each lookup O(1) and the whole solution O(n). 'Use a hash map' is the most common interview optimisation.