Hash Maps for Coding Interviews: A Crash Course

If you only master one data structure before an interview, make it the hash map. It turns slow searches into instant lookups, and it shows up in a huge share of array and string problems.

A hash map stores pairs of keys and values.

You give it a key, and it hands back the value stored for that key.

Python calls it a dict, Java a HashMap, and JavaScript a Map.

The idea and the costs are the same in every language.

1) Core Hash Map Operations

Almost everything you do with a hash map is one of these operations. Learn their costs, because the difference between O(1) and O(n) is usually what separates the brute force answer from the optimal one.

A Crash Course on Hash Maps

Instant lookups, explained in five slides.

O(1)

1. Insert, update or delete a pair

  • Insert: store a new key with its value, like Jin → Seoul.
  • Update: assigning to an existing key replaces its value.
  • Delete: remove a key and its value together.

Inserting "Jin"

O(1)

2. Check whether a key exists

  • Tells you if a key is stored without looking at every pair.
  • Average case is constant time, which is why maps beat lists for lookups.

Looking up "Maya"

O(n)

3. Search for a value

  • Maps are indexed by key, not by value.
  • Finding a value means scanning pair after pair.
  • If you need fast value lookups, keep a second map from value to key.

Searching for "Seoul"

O(n)

4. Iterate over every pair

  • Visits each key and value once.
  • Common after building a map, for example to find the most frequent element.

Visiting all 3 pairs

2) Three Ways Interviews Use Hash Maps

Most hash map problems are a variation of one of these three ideas.

1. Complement lookups

To find two numbers that add up to a target, remember each number you pass and ask whether its complement was already seen.

šŸ’” One pass instead of comparing every pair, so O(n) instead of O(n²).

In an interview:

Map each value to its index. For every new value, check if target - value is already a key.

def two_sum(nums, target):
    index_of = {}
    for i, x in enumerate(nums):
        if target - x in index_of:
            return [index_of[target - x], i]
        index_of[x] = i

2. Spotting repeats

Keep every element you have seen in a set. The moment an element is already in it, you found a duplicate.

šŸ’” Works for numbers, characters, coordinates, or any value you can hash.

In an interview:

Check membership first, then add. Return as soon as you find a repeat.

def has_repeat(items):
    seen = set()
    for item in items:
        if item in seen:
            return True
        seen.add(item)
    return False

3. Frequency counting

Map each element to the number of times it appears. Counts answer questions about anagrams, majority elements and the most common items.

šŸ’” Two strings are anagrams exactly when their counts are equal.

In an interview:

Start each count at 0 with get, then add one per occurrence.

counts = {}
for ch in "banana":
    counts[ch] = counts.get(ch, 0) + 1
# {"b": 1, "a": 3, "n": 2}

3) What Happens Under the Hood

A hash map is really an array of buckets plus a function that turns any key into a bucket index.

Hash function

A hash function turns a key into a large number. Taking that number modulo the array size gives a bucket index.

The same key always lands in the same bucket, so lookups go straight to it.

Buckets

The map keeps an array of buckets. Each pair lives in the bucket its key hashes to.

Reading or writing a bucket by index is O(1), which is where the speed comes from.

Collisions

Two different keys can hash to the same bucket. The usual fix is to keep a short list of pairs in that bucket.

With a good hash function the lists stay tiny, so operations stay O(1) on average.

Resizing

When too many pairs share too few buckets, the map allocates an array twice as large and re-inserts every pair.

One resize costs O(n), but it happens so rarely that each insert is still O(1) amortized.

NOTES

  • Average case lookups, inserts and deletes are O(1). The worst case is O(n) when many keys collide, which almost never matters in interviews.
  • Keys must be hashable. In Python that means immutable values such as numbers, strings and tuples, so use a tuple instead of a list as a key.
  • A set is a hash map that only stores keys.
  • Iteration order follows insertion order in modern Python, but do not rely on it in other languages.
  • Reach for a hash map whenever a problem asks whether you have seen something before, or how many times something appears.