AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started

Topic

Hash Table coding problems

A hash map turns "have I seen this before?" into a constant-time question.

New to hash table? Read the Hashing: Hash Maps and Hash Sets tutorial first.

439 problems · 134 easy · 250 medium · 55 hard · judged by hidden tests in 13 languages

439problems

134 easy

250 medium

55 hard

Sign in to tick off what you solve.

Study plan: 14 problems · about 8 h 10 min

Start the study plan

Next up: Contains Duplicate

How it works

Hash Table, step by step

Two Sum, with a hash map from value to index

nums = [7, 3, 9, 4, 12, 2], target = 14

nums7031924312425map: value → indexvalueindexemptytarget = 14

For each number, the partner it needs is 14 − nums[i]. A map from value to index answers "seen it before?" in O(1), so a single pass is enough; the map starts empty.

0:01 / 0:20Step 1 of 7
Transcript · 7 steps

Study plan

Learn hash table in 8 days

14 of the 439 Hash Table problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 425 in the full list below are practice at your own pace. Then move on to Counting.

  1. Day 1

    about 1 h 15 min

    Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

  2. Day 2

    about 55 min

    Medium problems: the same pattern with one twist each. Name the twist before you code.

  3. Day 3

    about 1 h 10 min

    More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

  4. Day 4

    about 1 h 10 min

    More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

  5. Day 5

    about 1 h 10 min

    More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

  6. Day 6

    about 50 min

    Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

  7. Day 7

    about 50 min

    Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

  8. Day 8

    about 50 min

    Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

    Next topic: Counting

Hash Table Coding Problems: 439 Questions with Solutions

439 hash table coding problems — 134 easy · 250 medium · 55 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 439
  • By difficulty: 134 easy · 250 medium · 55 hard
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
  • Cost: Free on every plan; sign in to run and submit

A hash map turns "have I seen this before?" into a constant-time question. The problems here are the classic exchanges of memory for time: counting occurrences, pairing complements (Two Sum), grouping anagrams, detecting duplicates, remembering the index of a value so a second pass can look back. Knowing when a map is the right tool — and when a fixed-size array or a set does the same job for less — is what they practise.

New to hash table? Read the Hashing: Hash Maps and Hash Sets tutorial first.

How hash table works, step by step

nums7031924312425imap: value → indexvalueindex70319243124need 12: map[12] = 4
Two Sum, with a hash map from value to index. Example: nums = [7, 3, 9, 4, 12, 2], target = 14
  1. For each number, the partner it needs is 14 − nums[i]. A map from value to index answers "seen it before?" in O(1), so a single pass is enough; the map starts empty.
  2. nums[0] = 7 needs 14 − 7 = 7. The map is still empty — looking before storing is what stops 7 pairing with itself — so store 7 → 0.
  3. nums[1] = 3 needs 11, which is not in the map, so no earlier number pairs with it. Store 3 → 1 for the numbers still to come.
  4. nums[2] = 9 needs 5: not in the map either, so store 9 → 2.
  5. nums[3] = 4 needs 10: not in the map either, so store 4 → 3.
  6. nums[4] = 12 needs 2: not in the map either, so store 12 → 4.
  7. nums[5] = 2 needs 14 − 2 = 12, and the map says 12 is at index 4: the answer is [4, 5]. One lookup and one store per number make it O(n) time and O(n) space, against O(n²) for every pair.

Hash Table study plan

14 of the 439 Hash Table problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 425 in the full list below are practice at your own pace. Then move on to Counting.

Day 1

Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 6

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

Day 7

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Day 8

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Next topic: Counting

Hash Table: the essentials

When to reach for it

The brute force has a nested loop whose inner loop only searches — for a complement, a duplicate, an earlier index, an equal key. Replace that search with a lookup. Other signals: "group by", "first unique", "how many times", or a need to remember where a value was last seen.

The pattern

Decide what the key is and what the value records. For pairing, check for the partner before storing the current element, so nothing pairs with itself. For grouping, compute a canonical key — a word's sorted letters, or its tuple of letter counts — and append to that key's list. Plain tallies have their own page: Counting.

def two_sum(nums, target):
    seen = {}                       # value -> index
    for i, x in enumerate(nums):
        if target - x in seen:
            return [seen[target - x], i]
        seen[x] = i
    return []

Cost

Expected O(1) per insert and lookup, so a pass is O(n) time and up to O(n) space. Two caveats: hashing a string key costs O(length), and many colliding keys make each operation linear — adversarial inputs can do that to C++'s unordered_map. Iteration order is not guaranteed by Java's HashMap or C++'s unordered_map; Python's dict and JavaScript's Map keep insertion order.

Common mistakes

  • Storing before checking, so x + x == target finds the element itself.
  • Using an array as a key: Python refuses a list, and Java hashes an int[] by identity, so two equal arrays miss each other. Use a tuple or a string.
  • A map where keys are small integers or lowercase letters; a plain array is faster.
  • Relying on the iteration order of a map that does not promise one.

Start with

All hash table problems

Easy (134)

Medium (250)

Hard (55)

Companies that ask hash table problems

  • Amazon 368 problems on hash table
  • Google 268 problems on hash table
  • Microsoft 53 problems on hash table
  • Meta 37 problems on hash table
  • Adobe 35 problems on hash table
  • TCS 25 problems on hash table
  • Infosys 23 problems on hash table
  • Bloomberg 19 problems on hash table
  • Uber 15 problems on hash table
  • Flipkart 12 problems on hash table
  • Atlassian 9 problems on hash table
  • Snapchat 9 problems on hash table

Next topic: Counting