Hash Table Coding Problems: 202 Questions with Solutions

202 hash table coding problems — 91 easy · 94 medium · 17 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 202
  • By difficulty: 91 easy · 94 medium · 17 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.

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 202 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 188 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 (91)

Medium (94)

Hard (17)

Companies that ask hash table problems

  • Amazon 171 problems on hash table
  • Google 109 problems on hash table
  • Adobe 33 problems on hash table
  • Microsoft 26 problems on hash table
  • TCS 24 problems on hash table
  • Infosys 23 problems on hash table
  • Meta 22 problems on hash table
  • Bloomberg 12 problems on hash table
  • Uber 11 problems on hash table
  • Cognizant 8 problems on hash table
  • Zoho 8 problems on hash table
  • Flipkart 7 problems on hash table

Next topic: Counting