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
nums = [7, 3, 9, 4, 12, 2], target = 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.
- 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.
- 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.
- nums[2] = 9 needs 5: not in the map either, so store 9 → 2.
- nums[3] = 4 needs 10: not in the map either, so store 4 → 3.
- nums[4] = 12 needs 2: not in the map either, so store 12 → 4.
- 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.
- Contains Duplicate Easy
- Two Sum Easy
- Jewels and Stones Easy
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.
- Permutation in String Medium
- Subarray Sum Equals K Medium
Day 5
More mediums. Before coding each one, write down what state the pattern keeps and when it changes.
- Contiguous Array Medium
- Continuous Subarray Sum Medium
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.
- Word Ladder Hard
Day 8
Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.
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 == targetfinds 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
- Two Sum: the complement lookup in its simplest form.
- Contains Duplicate: a set as the "seen before" test.
- Longest Consecutive Sequence: O(n) where sorting would be O(n log n).
All hash table problems
Easy (91)
- Find the Losers of the Circular Game Array, Simulation
- Form Smallest Number From Two Digit Arrays Array, Enumeration
- Most Frequent Even Element Array, Counting
- Rearrange Characters to Make Target String String, Counting
- Decode the Message String
- Existence of a Substring in a String and Its Reverse String
- Find the XOR of Numbers Which Appear Twice Array, Bit Manipulation
- Number of Beautiful Pairs Array, Math, Counting
- Check if Array is Good Array, Sorting
- Split the Array Array, Counting
- Best Poker Hand Array, Counting, Greedy
- Substrings of Size Three with Distinct Characters String, Sliding Window, Counting
- Count Number of Pairs With Absolute Difference K Array, Counting
- Intersection of Multiple Arrays Array, Counting
- Check if Number Has Equal Digit Count and Digit Value String, Counting
- Number of Distinct Averages Array, Two Pointers, Sorting
- Check if The Number is Fascinating Math, String
- Counting Elements Array, Counting
- Largest Unique Number Array, Sorting
- Rings and Rods String, Bit Manipulation
- Check if All Characters Have Equal Number of Occurrences String, Counting
- Find Words That Can Be Formed by Characters String, Counting
- Most Common Word String
- Largest Substring Between Two Equal Characters String
- Count Pairs With Given Sum Array
- Sort the People Array, Sorting
- Find Missing and Repeated Values Matrix, Math
- Minimum Operations to Collect Elements Array, Simulation
- Find the Distinct Difference Array Array, Prefix Sum
- The Two Sneaky Numbers of Digitville Array
- Find the Difference of Two Arrays Array
- Largest Positive Integer That Exists With Its Negative Array, Two Pointers
- Maximum Number of Pairs in Array Array, Counting
- Rank Transform of an Array Array, Sorting
- N-Repeated Element in Size 2N Array Array
- Find Common Characters Array, String
- Keyboard Row Array, String
- Minimum Index Sum of Two Lists Array, String
- Count Elements With Maximum Frequency Array, Counting
- Sum of Unique Elements Array, Counting
- Unique Number of Occurrences Array
- Maximum Number of Balls in a Box Math, Counting
- Permutation Difference between Two Strings String
- Odd String Difference Array, String
- First Letter to Appear Twice String
- Count Common Words With One Occurrence Array, String
- Maximum Number of Words You Can Type String
- Number of Different Integers in a String String
- Verifying an Alien Dictionary Array, String
- Unique Morse Code Words Array, String
- Divide Array Into Equal Pairs Array, Bit Manipulation
- Count Largest Group Math
- Find Lucky Integer in an Array Array, Counting
- Fair Candy Swap Array
- Count the Number of Consistent Strings Array, String, Bit Manipulation
- Find the Difference String, Bit Manipulation, Sorting
- Sort Array by Increasing Frequency Array, Sorting
- Find Winner on a Tic Tac Toe Game Array, Matrix, Simulation
- Number of Equivalent Domino Pairs Array, Counting
- Number of Arithmetic Triplets Array, Two Pointers, Enumeration
- Minimum Common Value Array, Two Pointers, Binary Search
- Check If N and Its Double Exist Array, Two Pointers, Sorting
- Maximum Number of Balloons String, Counting
- Unique Email Addresses Array, String
- Buddy Strings String
- Check if the Sentence Is Pangram String
- Jewels and Stones String
- Longest Palindrome String, Greedy
- Ransom Note String, Counting
- First Unique Character in a String String, Queue, Counting
- Relative Sort Array Array, Sorting, Counting Sort
- Distribute Candies Array, Greedy
- Longest Harmonious Subsequence Array, Sorting, Sliding Window
- Degree of an Array Array
- Number of Good Pairs Array, Math, Counting
- How Many Numbers Are Smaller Than the Current Number Array, Counting Sort
- Set Mismatch Array, Bit Manipulation, Sorting
- Intersection of Two Arrays II Array, Two Pointers, Sorting
- Intersection of Two Arrays Array, Two Pointers, Sorting
- Contains Duplicate II Array, Sliding Window
- Roman to Integer Math, String
- Happy Number Math, Two Pointers
- Find the Town Judge Graph
- Next Greater Element I Array, Stack
- Word Pattern String
- Isomorphic Strings String
- Find All Numbers Disappeared in an Array Array
- Majority Element Array, Divide and Conquer
- Contains Duplicate Array, Sorting
- Valid Anagram String, Sorting, Counting
- Two Sum Array
Medium (94)
- Count Number of Texts Math, String, Dynamic Programming
- Construct String With Repeat Limit String, Greedy, Heap (Priority Queue)
- Minimum Number of Steps to Make Two Strings Anagram II String, Counting
- Count Vowel Substrings of a String String, Sliding Window
- Minimum Length of String After Operations String, Counting
- Sum of Digit Differences of All Pairs Array, Math, Counting
- The Number of the Smallest Unoccupied Chair Array, Heap (Priority Queue), Ordered Set
- Number of Nodes in the Sub-Tree With the Same Label Tree, Graph, Depth-First Search
- Convert an Array Into a 2D Array With Conditions Array, Matrix
- First Completely Painted Row or Column Array, Matrix
- The Number of Beautiful Subsets Array, Dynamic Programming, Backtracking
- Longest Ideal Subsequence String, Dynamic Programming
- Longest Arithmetic Subsequence Array, Dynamic Programming, Binary Search
- Destroy Sequential Targets Array, Counting
- Smallest Missing Non-negative Integer After Operations Array, Math, Greedy
- Minimum Consecutive Cards to Pick Up Array, Sliding Window
- Length of Longest Subarray With at Most K Frequency Array, Sliding Window
- Count Complete Subarrays in an Array Array, Sliding Window
- Count the Number of Good Subarrays Array, Sliding Window, Counting
- Swap For Longest Repeated Character Substring String, Sliding Window, Binary Search
- Number of Substrings Containing All Three Characters String, Sliding Window
- Longest Substring with At Most K Distinct Characters String, Sliding Window
- Longest Substring with At Most Two Distinct Characters String, Sliding Window
- 3Sum With Multiplicity Array, Two Pointers, Counting
- Max Number of K-Sum Pairs Array, Two Pointers, Sorting
- Number of Wonderful Substrings Bit Manipulation, String, Prefix Sum
- Find the Longest Substring Containing Vowels in Even Counts Bit Manipulation, String, Prefix Sum
- Minimum Number of Operations to Make Array Empty Greedy, Counting
- 4Sum II Array, Counting
- Find Players With Zero or One Losses Array, Sorting, Counting
- Minimum Area Rectangle Array, Geometry
- Number of Boomerangs Math, Array
- Group the People Given the Group Size They Belong To Array, Greedy
- Optimal Partition of String String, Greedy
- Count Number of Bad Pairs Array, Counting
- Determine if Two Strings Are Close String, Counting, Sorting
- Equal Row and Column Pairs Matrix, Simulation
- Brick Wall Array, Counting
- Smallest String With Swaps Union Find, String, Sorting
- Number of Pairs of Interchangeable Rectangles Array, Math, Counting
- Count Nice Pairs in an Array Array, Math, Counting
- Maximum Size Subarray Sum Equals k Array, Prefix Sum
- Longest Well-Performing Interval Array, Prefix Sum, Stack
- Subarray Sums Divisible by K Array, Prefix Sum
- Maximum Number of Integers to Choose From a Range I Greedy, Binary Search
- Short Encoding of Words String, Trie
- Replace Words String, Trie
- Longest Word in Dictionary String, Trie
- Word Subsets String, Counting
- Number of Good Ways to Split a String String, Prefix Sum
- Can Convert String in K Moves String, Greedy
- Maximum Number of Occurrences of a Substring String, Sliding Window
- Count Words Obtained After Adding a Letter String, Bit Manipulation
- Find and Replace Pattern String
- Longest Subarray With Sum Zero Array, Prefix Sum
- Find Original Array From Doubled Array Array, Greedy, Sorting
- K-diff Pairs in an Array Array
- Minimum Genetic Mutation String, Breadth-First Search
- Number of Distinct Islands Array, Depth-First Search, Breadth-First Search
- Count Triplets That Can Form Two Arrays of Equal XOR Array, Math, Bit Manipulation
- Maximum XOR of Two Numbers in an Array Array, Bit Manipulation, Trie
- Top K Frequent Words Array, String, Sorting
- Minimum Number of Steps to Make Two Strings Anagram String, Counting
- Minimum Deletions to Make Character Frequencies Unique String, Greedy, Sorting
- Reduce Array Size to The Half Array, Greedy, Sorting
- Longest String Chain Array, Two Pointers, String
- Ugly Number II Math, Dynamic Programming, Heap
- Longest Arithmetic Subsequence of Given Difference Array, Dynamic Programming
- Delete and Earn Array, Dynamic Programming
- Valid Sudoku Array, Matrix
- Count Number of Nice Subarrays Array, Math, Sliding Window
- Binary Subarrays With Sum Array, Sliding Window, Prefix Sum
- Maximum Erasure Value Array, Sliding Window
- Minimum Operations to Reduce X to Zero Array, Binary Search, Sliding Window
- Continuous Subarray Sum Array, Math, Prefix Sum
- Contiguous Array Array, Prefix Sum
- Custom Sort String String, Sorting
- Majority Element II Array, Sorting, Counting
- Find All Duplicates in an Array Array
- Integer to Roman Math, String, Greedy
- Letter Combinations of a Phone Number String, Backtracking
- Set Matrix Zeroes Array, Matrix
- Sort Characters By Frequency String, Heap, Bucket Sort
- Least Number of Unique Integers after K Removals Array, Heap, Greedy
- Top K Frequent Elements Array, Heap, Bucket Sort
- Word Break String, Dynamic Programming, Trie
- Partition Labels String, Greedy, Two Pointers
- Hand of Straights Array, Greedy, Sorting
- Permutation in String String, Sliding Window
- Fruit Into Baskets Array, Sliding Window
- Find All Anagrams in a String String, Sliding Window
- Longest Substring Without Repeating Characters String, Sliding Window
- Longest Consecutive Sequence Array, Union Find
- Subarray Sum Equals K Array, Prefix Sum
Hard (17)
- Minimum Cost to Split an Array Array, Dynamic Programming, Counting
- Count Anagrams Math, String, Combinatorics
- Smallest Range Covering Elements from K Lists Array, Greedy, Sliding Window
- Minimum Number of Operations to Make Array Continuous Array, Binary Search, Sliding Window
- Bus Routes Array, Breadth-First Search
- Jump Game IV Array, Breadth-First Search
- Grid Illumination Array, Matrix
- Number of Submatrices That Sum to Target Array, Matrix, Prefix Sum
- Frog Jump Array, Dynamic Programming
- Take K of Each Character From Left and Right String, Sliding Window, Prefix Sum
- Subarrays with K Different Integers Array, Sliding Window, Two Pointers
- Substring with Concatenation of All Words String, Sliding Window
- Max Points on a Line Math, Geometry
- Count Unique Characters of All Substrings of a Given String String, Counting
- Word Ladder String, Breadth-First Search
- Minimum Window Substring String, Sliding Window
- First Missing Positive Array
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