Hashing Practice Problems
Hashing Practice Problems
Master hashing techniques through carefully curated practice problems. Start with easy problems and progress to advanced challenges.
How to Use This Guide
- ›Start with Easy - Build fundamentals
- ›Progress to Medium - Learn patterns
- ›Challenge with Hard - Master techniques
- ›Review Common Patterns - Recognize similar problems
Pro Tip: Try to solve each problem yourself before looking at hints. If stuck after 20 minutes, read the hint and try again.
Problem Categories
| Category | Count | Patterns |
|---|---|---|
| Frequency Counting | 8 | Hash map, Counter |
| Two Sum Variations | 7 | Hash map, Two pointers |
| Subarray Problems | 9 | Prefix sum, Hash map |
| Set Operations | 6 | Hash set, Membership |
| String Hashing | 8 | Character map, Anagrams |
| Design Problems | 4 | Hash map + List/Set |
Easy Problems
1. Two Sum
LeetCode: #1 | Difficulty: Easy | Acceptance: 49%
Problem:
Given an array of integers nums and an integer target, return indices of two numbers that add up to target.
Example:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: nums[0] + nums[1] = 2 + 7 = 9
Constraints:
- ›Each input has exactly one solution
- ›Cannot use same element twice
Hint: Use hash map to store value → index. For each number, check if (target - number) exists in map.
Approach:
- ›Time: O(n), Space: O(n)
- ›Pattern: Hash map lookup
2. Contains Duplicate
LeetCode: #217 | Difficulty: Easy | Acceptance: 61%
Problem:
Return true if any value appears at least twice in the array.
Example:
Input: nums = [1,2,3,1]
Output: true
Input: nums = [1,2,3,4]
Output: false
Hint: Use a set. If element already in set, return true. Otherwise, add it.
Approach:
- ›Time: O(n), Space: O(n)
- ›Pattern: Hash set for duplicate detection
3. Valid Anagram
LeetCode: #242 | Difficulty: Easy | Acceptance: 63%
Problem:
Given two strings s and t, return true if t is an anagram of s.
Example:
Input: s = "anagram", t = "nagaram"
Output: true
Input: s = "rat", t = "car"
Output: false
Hint: Count character frequencies in both strings and compare.
Approach:
- ›Time: O(n), Space: O(1) - only 26 letters
- ›Pattern: Frequency map
4. Intersection of Two Arrays
LeetCode: #349 | Difficulty: Easy | Acceptance: 72%
Problem:
Find the intersection of two arrays. Each element in result must be unique.
Example:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4] or [4,9]
Hint: Convert both arrays to sets, then find intersection.
Approach:
- ›Time: O(n + m), Space: O(n + m)
- ›Pattern: Set intersection
5. First Unique Character
LeetCode: #387 | Difficulty: Easy | Acceptance: 59%
Problem:
Find the first non-repeating character in a string and return its index. If none exists, return -1.
Example:
Input: s = "leetcode"
Output: 0
Input: s = "loveleetcode"
Output: 2
Input: s = "aabb"
Output: -1
Hint: Two passes: count frequencies, then find first character with frequency 1.
Approach:
- ›Time: O(n), Space: O(1) - max 26 letters
- ›Pattern: Frequency counting
6. Majority Element
LeetCode: #169 | Difficulty: Easy | Acceptance: 64%
Problem:
Find the element that appears more than ⌊n/2⌋ times.
Example:
Input: nums = [3,2,3]
Output: 3
Input: nums = [2,2,1,1,1,2,2]
Output: 2
Hint: Count frequencies and track element with highest count. Or use Boyer-Moore voting algorithm.
Approach:
- ›Time: O(n), Space: O(n) with hash map
- ›Pattern: Frequency counting
7. Happy Number
LeetCode: #202 | Difficulty: Easy | Acceptance: 55%
Problem:
A happy number is defined by: repeatedly replace number with sum of squares of its digits until it equals 1, or loops endlessly. Return true if happy.
Example:
Input: n = 19
Output: true
Explanation:
1² + 9² = 82
8² + 2² = 68
6² + 8² = 100
1² + 0² + 0² = 1
Hint: Use set to detect cycles. If number seen before, it's a cycle.
Approach:
- ›Time: O(log n), Space: O(log n)
- ›Pattern: Cycle detection with set
8. Isomorphic Strings
LeetCode: #205 | Difficulty: Easy | Acceptance: 44%
Problem:
Two strings are isomorphic if characters in s can be replaced to get t.
Example:
Input: s = "egg", t = "add"
Output: true
Input: s = "foo", t = "bar"
Output: false
Hint: Use two hash maps to track character mappings in both directions.
Approach:
- ›Time: O(n), Space: O(1)
- ›Pattern: Bidirectional mapping
Medium Problems
9. Group Anagrams
LeetCode: #49 | Difficulty: Medium | Acceptance: 67%
Problem:
Group strings that are anagrams of each other.
Example:
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Hint: Use sorted string as key in hash map. All anagrams will have same sorted form.
Approach:
- ›Time: O(n × k log k) where k = max string length
- ›Space: O(n × k)
- ›Pattern: Hash map with computed key
10. Top K Frequent Elements
LeetCode: #347 | Difficulty: Medium | Acceptance: 66%
Problem:
Return the k most frequent elements.
Example:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Input: nums = [1], k = 1
Output: [1]
Hint: Count frequencies with hash map, then use heap or bucket sort.
Approach:
- ›Time: O(n log k) with heap
- ›Space: O(n)
- ›Pattern: Frequency + heap
11. Longest Substring Without Repeating Characters
LeetCode: #3 | Difficulty: Medium | Acceptance: 33%
Problem:
Find length of longest substring without repeating characters.
Example:
Input: s = "abcabcbb"
Output: 3
Explanation: "abc"
Input: s = "pwwkew"
Output: 3
Explanation: "wke"
Hint: Sliding window + hash set to track characters in current window.
Approach:
- ›Time: O(n), Space: O(min(n, m)) where m = charset size
- ›Pattern: Sliding window + set
12. Subarray Sum Equals K
LeetCode: #560 | Difficulty: Medium | Acceptance: 44%
Problem:
Find total number of continuous subarrays whose sum equals k.
Example:
Input: nums = [1,1,1], k = 2
Output: 2
Explanation: [1,1] and [1,1]
Input: nums = [1,2,3], k = 3
Output: 2
Explanation: [1,2] and [3]
Hint: Use prefix sum + hash map. Count occurrences of (prefix_sum - k).
Approach:
- ›Time: O(n), Space: O(n)
- ›Pattern: Prefix sum + hash map
13. Longest Consecutive Sequence
LeetCode: #128 | Difficulty: Medium | Acceptance: 48%
Problem:
Find length of longest consecutive elements sequence in O(n) time.
Example:
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: [1,2,3,4]
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
Hint: Put all numbers in set. For each number, check if it's start of sequence (no num-1), then count consecutive numbers.
Approach:
- ›Time: O(n), Space: O(n)
- ›Pattern: Hash set for O(1) lookup
14. 3Sum
LeetCode: #15 | Difficulty: Medium | Acceptance: 33%
Problem:
Find all unique triplets that sum to zero.
Example:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Hint: Sort array, fix first element, then use two pointers on remaining.
Approach:
- ›Time: O(n²), Space: O(1)
- ›Pattern: Sort + two pointers
15. 4Sum
LeetCode: #18 | Difficulty: Medium | Acceptance: 36%
Problem:
Find all unique quadruplets that sum to target.
Example:
Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Hint: Similar to 3Sum. Fix two elements, then two pointers.
Approach:
- ›Time: O(n³), Space: O(1)
- ›Pattern: Sort + nested iteration + two pointers
16. Contiguous Array
LeetCode: #525 | Difficulty: Medium | Acceptance: 47%
Problem:
Find maximum length subarray with equal number of 0s and 1s.
Example:
Input: nums = [0,1]
Output: 2
Explanation: [0,1]
Input: nums = [0,1,0]
Output: 2
Explanation: [0,1] or [1,0]
Hint: Convert 0 to -1, then find longest subarray with sum 0.
Approach:
- ›Time: O(n), Space: O(n)
- ›Pattern: Transform + prefix sum
17. Valid Sudoku
LeetCode: #36 | Difficulty: Medium | Acceptance: 58%
Problem:
Determine if a 9×9 Sudoku board is valid.
Example:
Input: board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: true
Hint: Use three sets: rows, columns, boxes. Check each cell against corresponding sets.
Approach:
- ›Time: O(1) - board is 9×9
- ›Space: O(1)
- ›Pattern: Multiple hash sets
Hard Problems
18. Longest Substring with At Most K Distinct Characters
LeetCode: #340 (Premium) | Difficulty: Hard
Problem:
Find length of longest substring with at most K distinct characters.
Example:
Input: s = "eceba", k = 2
Output: 3
Explanation: "ece"
Input: s = "aa", k = 1
Output: 2
Hint: Sliding window with hash map to track character frequencies.
Approach:
- ›Time: O(n), Space: O(k)
- ›Pattern: Sliding window + hash map
19. Count of Range Sum
LeetCode: #327 | Difficulty: Hard | Acceptance: 37%
Problem:
Count number of range sums that lie in [lower, upper].
Example:
Input: nums = [-2,5,-1], lower = -2, upper = 2
Output: 3
Explanation: [0,0], [2,2], [0,2] (indices)
Hint: Use prefix sums. For each position, count how many previous prefix sums create valid range.
Approach:
- ›Time: O(n log n) with merge sort
- ›Space: O(n)
- ›Pattern: Prefix sum + divide & conquer
20. Substring with Concatenation of All Words
LeetCode: #30 | Difficulty: Hard | Acceptance: 31%
Problem:
Find all starting indices of substring(s) that is concatenation of all words.
Example:
Input: s = "barfoothefoobarman", words = ["foo","bar"]
Output: [0,9]
Explanation:
"barfoo" starts at index 0
"foobar" starts at index 9
Hint: Sliding window with two hash maps: expected word counts and current window counts.
Approach:
- ›Time: O(n × m × len) where n = string length, m = word count
- ›Space: O(m)
- ›Pattern: Sliding window + hash map
21. Minimum Window Substring
LeetCode: #76 | Difficulty: Hard | Acceptance: 41%
Problem:
Find minimum window in s that contains all characters of t.
Example:
Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Hint: Sliding window with two hash maps: required chars and window chars.
Approach:
- ›Time: O(n + m), Space: O(1) - limited charset
- ›Pattern: Sliding window + hash map
22. Max Points on a Line
LeetCode: #149 | Difficulty: Hard | Acceptance: 21%
Problem:
Find maximum number of points that lie on the same straight line.
Example:
Input: points = [[1,1],[2,2],[3,3]]
Output: 3
Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Hint: For each point, calculate slopes to all other points. Use hash map to count points with same slope.
Approach:
- ›Time: O(n²), Space: O(n)
- ›Pattern: Hash map with slope as key
23. Alien Dictionary
LeetCode: #269 (Premium) | Difficulty: Hard
Problem:
Derive order of letters in alien language from sorted dictionary of words.
Example:
Input: words = ["wrt","wrf","er","ett","rftt"]
Output: "wertf"
Hint: Build graph of character order dependencies, then topological sort.
Approach:
- ›Time: O(C) where C = total characters
- ›Space: O(1) - limited alphabet
- ›Pattern: Graph + topological sort + hash map
24. Find All Anagrams in a String
LeetCode: #438 | Difficulty: Medium | Acceptance: 50%
Problem:
Find all start indices of p's anagrams in s.
Example:
Input: s = "cbaebabacd", p = "abc"
Output: [0,6]
Explanation:
"cba" at index 0
"bac" at index 6
Hint: Sliding window of size len(p). Compare character frequencies.
Approach:
- ›Time: O(n), Space: O(1)
- ›Pattern: Sliding window + frequency map
Problem Patterns Summary
Frequency Counting
- ›Two Sum (#1)
- ›Valid Anagram (#242)
- ›First Unique Character (#387)
- ›Majority Element (#169)
- ›Top K Frequent Elements (#347)
- ›Group Anagrams (#49)
Subarray with Hash Map
- ›Subarray Sum Equals K (#560)
- ›Contiguous Array (#525)
- ›Longest Substring Without Repeating (#3)
- ›Count of Range Sum (#327)
Two Pointers + Hash
- ›3Sum (#15)
- ›4Sum (#18)
- ›Two Sum II (#167)
Sliding Window + Hash
- ›Longest Substring K Distinct (#340)
- ›Minimum Window Substring (#76)
- ›Find All Anagrams (#438)
Set Operations
- ›Contains Duplicate (#217)
- ›Intersection of Two Arrays (#349)
- ›Longest Consecutive Sequence (#128)
- ›Happy Number (#202)
Design with Hash
- ›LRU Cache (#146)
- ›Insert Delete GetRandom O(1) (#380)
- ›Time Based Key-Value Store (#981)
Practice Plan
Week 1: Easy Problems (Days 1-3)
- ›Day 1: #1, #217, #242
- ›Day 2: #349, #387, #169
- ›Day 3: #202, #205
Week 2: Medium Problems (Days 4-7)
- ›Day 4: #49, #347, #3
- ›Day 5: #560, #128
- ›Day 6: #15, #525
- ›Day 7: #36, Review Easy
Week 3: Advanced Medium (Days 8-10)
- ›Day 8: #18, #438
- ›Day 9: Review all Medium
- ›Day 10: Timed practice - 3 Medium in 90 min
Week 4: Hard Problems (Days 11-14)
- ›Day 11: #340, #76
- ›Day 12: #327, #30
- ›Day 13: #149, #269
- ›Day 14: Final review - Mix of all difficulties
Time Complexity Patterns
| Pattern | Time | Space | Problems |
|---|---|---|---|
| Hash map lookup | O(n) | O(n) | #1, #217, #560 |
| Frequency counting | O(n) | O(k) | #242, #347, #49 |
| Sliding window | O(n) | O(k) | #3, #76, #438 |
| Two pointers | O(n²) | O(1) | #15, #18 |
| Prefix sum | O(n) | O(n) | #560, #525 |
Tips for Success
Before Starting:
- ›Read problem carefully twice
- ›Identify input/output types
- ›Note constraints (size limits, ranges)
- ›Ask clarifying questions
While Solving:
- ›Start with brute force approach
- ›Identify bottlenecks
- ›Think: can hash map help?
- ›Draw examples for clarity
Common Hash Map Uses:
- ›Store value → index
- ›Count frequencies
- ›Track seen elements
- ›Store prefix sums
- ›Map transformations
After Solving:
- ›Test edge cases (empty, single element)
- ›Analyze time/space complexity
- ›Can it be optimized further?
- ›Review similar problems
Additional Resources
LeetCode Lists
- ›Easy Hash Table: Filter by tag "Hash Table" + Easy
- ›Medium Hash Table: 100+ problems
- ›Interview Prep: Top Interview Questions - Hash Table
Study Resources
- ›Patterns: Study frequency, subarray, two sum patterns
- ›Templates: Save working code templates
- ›Mock Interviews: Practice explaining solutions aloud
Progress Tracker
Track your progress:
Easy (8 total):
- › Two Sum (#1)
- › Contains Duplicate (#217)
- › Valid Anagram (#242)
- › Intersection (#349)
- › First Unique Char (#387)
- › Majority Element (#169)
- › Happy Number (#202)
- › Isomorphic Strings (#205)
Medium (10 total):
- › Group Anagrams (#49)
- › Top K Frequent (#347)
- › Longest Substring (#3)
- › Subarray Sum K (#560)
- › Longest Consecutive (#128)
- › 3Sum (#15)
- › 4Sum (#18)
- › Contiguous Array (#525)
- › Valid Sudoku (#36)
- › Find Anagrams (#438)
Hard (6 total):
- › K Distinct (#340)
- › Range Sum Count (#327)
- › Concat All Words (#30)
- › Min Window (#76)
- › Max Points Line (#149)
- › Alien Dictionary (#269)
Start with Easy problems and progress systematically. Consistency beats intensity! 🚀