hash-table-problems.
md 2026-04-05
Hash Table Problems (30 Problems)
Overview
Difficulty Count
Easy 5
Medium 25
Total 30
Easy Problems (5)
1. Two Sum (#1)
Link: [Link]
Problem: Given array nums and target, return indices of two numbers that add up to target.
Example: nums = [2,7,11,15], target = 9 → [0,1]
Hash Insight: Store {value: index}, check if target - num exists.
2. Contains Duplicate (#217)
Link: [Link]
Problem: Return true if any value appears at least twice in array.
Example: nums = [1,2,3,1] → true
Hash Insight: HashSet to track seen elements. O(n) time, O(n) space.
3. Happy Number (#202)
Link: [Link]
Problem: Determine if number is "happy" (sum of squares of digits eventually equals 1).
Example: n = 19 → true (1² + 9² = 82 → 8² + 2² = 68 → ... → 1)
Hash Insight: HashSet to detect cycles. If sum repeats, not happy.
4. Isomorphic Strings (#205)
Link: [Link]
Problem: Two strings are isomorphic if characters can be replaced to get the other.
1/9
[Link] 2026-04-05
Example: s = "egg", t = "add" → true (e→a, g→d)
Hash Insight: Two hash maps for bidirectional character mapping.
5. Contains Duplicate II (#219)
Link: [Link]
Problem: Return true if nums[i] == nums[j] and abs(i - j) <= k.
Example: nums = [1,2,3,1], k = 3 → true
Hash Insight: HashMap {value: lastIndex}, check distance when duplicate found.
Medium Problems (25)
6. Longest Substring Without Repeating Characters (#3)
Link: [Link]
Problem: Find length of longest substring without repeating characters.
Example: s = "abcabcbb" → 3 ("abc")
Hash Insight: Sliding window + HashSet/HashMap to track characters in window.
7. Group Anagrams (#49)
Link: [Link]
Problem: Group strings that are anagrams of each other.
Example: ["eat","tea","tan","ate","nat","bat"] → [["bat"],["nat","tan"],
["ate","eat","tea"]]
Hash Insight: HashMap with sorted string or character count as key.
8. Valid Sudoku (#36)
Link: [Link]
Problem: Determine if a 9x9 Sudoku board is valid (no duplicates in rows, cols, boxes).
Example: Check each row, column, and 3x3 box for duplicates.
Hash Insight: Three HashSets per row, column, and box. Or encode as "row-num", "col-num", "box-num".
9. Longest Consecutive Sequence (#128)
Link: [Link]
2/9
[Link] 2026-04-05
Problem: Find length of longest consecutive elements sequence in O(n) time.
Example: nums = [100,4,200,1,3,2] → 4 (sequence: 1,2,3,4)
Hash Insight: HashSet for O(1) lookup. Only start counting from sequence start (no num-1 exists).
10. LRU Cache (#146)
Link: [Link]
Problem: Design LRU cache with O(1) get and put operations.
Example: get(key) returns value, put(key, value) evicts least recently used if full.
Hash Insight: HashMap + Doubly Linked List. Map stores key → node pointer.
11. Integer to Roman (#12)
Link: [Link]
Problem: Convert integer to Roman numeral.
Example: num = 1994 → "MCMXCIV"
Hash Insight: HashMap/array of value-symbol pairs, greedy subtraction.
12. Set Matrix Zeroes (#73)
Link: [Link]
Problem: If element is 0, set entire row and column to 0 (in-place).
Example: [[1,1,1],[1,0,1],[1,1,1]] → [[1,0,1],[0,0,0],[1,0,1]]
Hash Insight: Use first row/col as markers instead of extra HashSets. O(1) space.
13. Clone Graph (#133)
Link: [Link]
Problem: Deep copy an undirected graph.
Example: Clone all nodes and their neighbor connections.
Hash Insight: HashMap {originalNode: clonedNode} to avoid duplicating nodes.
14. Copy List with Random Pointer (#138)
Link: [Link]
3/9
[Link] 2026-04-05
Problem: Deep copy linked list where each node has a random pointer.
Example: Clone nodes with both next and random pointers preserved.
Hash Insight: HashMap {original: clone} for O(n) space, or interleave nodes for O(1) space.
15. Word Break (#139)
Link: [Link]
Problem: Return true if string can be segmented into dictionary words.
Example: s = "leetcode", wordDict = ["leet","code"] → true
Hash Insight: HashSet for O(1) word lookup + DP or BFS.
16. Repeated DNA Sequences (#187)
Link: [Link]
Problem: Find all 10-letter sequences that occur more than once.
Example: s = "AAAAACCCCCAAAAACCCCCC" → ["AAAAACCCCC","CCCCCAAAAA"]
Hash Insight: HashMap to count substrings, or rolling hash for optimization.
17. Implement Trie (#208)
Link: [Link]
Problem: Implement Trie with insert, search, and startsWith.
Example: insert("apple"), search("app") → false, startsWith("app") → true
Hash Insight: Each node has HashMap of children {char: TrieNode}.
18. Majority Element II (#229)
Link: [Link]
Problem: Find all elements appearing more than ⌊n/3⌋ times.
Example: nums = [3,2,3] → [3]
Hash Insight: HashMap for counting, or Boyer-Moore for O(1) space.
19. Bulls and Cows (#299)
Link: [Link]
4/9
[Link] 2026-04-05
Problem: Return hint "xAyB" where x = bulls (correct position), y = cows (wrong position).
Example: secret = "1807", guess = "7810" → "1A3B"
Hash Insight: HashMap/array to count digit frequencies for cows calculation.
20. Fraction to Recurring Decimal (#166)
Link: [Link]
Problem: Convert fraction to decimal string, with repeating part in parentheses.
Example: 4/333 → "0.(012)"
Hash Insight: HashMap {remainder: position} to detect repeating cycle.
21. Subarray Sum Equals K (#560)
Link: [Link]
Problem: Count subarrays with sum equal to k.
Example: nums = [1,1,1], k = 2 → 2
Hash Insight: HashMap {prefixSum: count}. Check if prefixSum - k exists.
22. Top K Frequent Elements (#347)
Link: [Link]
Problem: Return k most frequent elements.
Example: nums = [1,1,1,2,2,3], k = 2 → [1,2]
Hash Insight: HashMap for frequency count + bucket sort or heap.
23. 4Sum II (#454)
Link: [Link]
Problem: Count tuples (i,j,k,l) where nums1[i] + nums2[j] + nums3[k] + nums4[l] = 0.
Example: Four arrays, find zero-sum combinations.
Hash Insight: HashMap {a+b: count} for first two arrays, lookup -(c+d).
24. Find All Anagrams in a String (#438)
Link: [Link]
5/9
[Link] 2026-04-05
Problem: Find all start indices of p's anagrams in s.
Example: s = "cbaebabacd", p = "abc" → [0,6]
Hash Insight: Sliding window + HashMap/array for character counts.
25. Contiguous Array (#525)
Link: [Link]
Problem: Find max length of contiguous subarray with equal 0s and 1s.
Example: nums = [0,1,0] → 2
Hash Insight: Treat 0 as -1, use prefix sum. HashMap {sum: firstIndex}.
26. Brick Wall (#554)
Link: [Link]
Problem: Draw vertical line crossing minimum bricks.
Example: Find position with most edge alignments.
Hash Insight: HashMap {edgePosition: count}. Answer = rows - maxEdges.
27. Encode and Decode TinyURL (#535)
Link: [Link]
Problem: Design URL shortening service.
Example: Encode long URL to short, decode back to original.
Hash Insight: Two HashMaps for bidirectional mapping, or single map with generated key.
28. Insert Delete GetRandom O(1) (#380)
Link: [Link]
Problem: Design data structure with O(1) insert, delete, and getRandom.
Example: All operations in constant time, random with equal probability.
Hash Insight: HashMap {val: index} + ArrayList. Swap with last for O(1) delete.
29. Sort Characters By Frequency (#451)
Link: [Link]
6/9
[Link] 2026-04-05
Problem: Sort string by character frequency (descending).
Example: s = "tree" → "eert" or "eetr"
Hash Insight: HashMap for frequency count + bucket sort or heap.
30. Maximum Size Subarray Sum Equals k (#325)
Link: [Link]
Problem: Find max length subarray with sum equal to k.
Example: nums = [1,-1,5,-2,3], k = 3 → 4 (subarray [1,-1,5,-2])
Hash Insight: HashMap {prefixSum: firstIndex}. Length = i - map[prefixSum - k].
Quick Reference Table
# Problem LC# Difficulty Key Pattern
1 Two Sum 1 Easy Complement lookup
2 Contains Duplicate 217 Easy Seen set
3 Happy Number 202 Easy Cycle detection
4 Isomorphic Strings 205 Easy Bidirectional mapping
5 Contains Duplicate II 219 Easy Index tracking
6 Longest Substring 3 Medium Sliding window
7 Group Anagrams 49 Medium Sorted key
8 Valid Sudoku 36 Medium Multi-set tracking
9 Longest Consecutive 128 Medium Sequence start
10 LRU Cache 146 Medium Map + DLL
11 Integer to Roman 12 Medium Value-symbol map
12 Set Matrix Zeroes 73 Medium In-place markers
13 Clone Graph 133 Medium Node mapping
14 Copy List Random 138 Medium Node mapping
15 Word Break 139 Medium Word set + DP
16 Repeated DNA 187 Medium Substring count
17 Implement Trie 208 Medium Children map
18 Majority Element II 229 Medium Frequency count
7/9
[Link] 2026-04-05
# Problem LC# Difficulty Key Pattern
19 Bulls and Cows 299 Medium Digit frequency
20 Fraction to Decimal 166 Medium Remainder tracking
21 Subarray Sum K 560 Medium Prefix sum count
22 Top K Frequent 347 Medium Freq + bucket
23 4Sum II 454 Medium Pair sum map
24 Find Anagrams 438 Medium Sliding window
25 Contiguous Array 525 Medium Prefix sum
26 Brick Wall 554 Medium Edge count
27 TinyURL 535 Medium Bidirectional map
28 RandomizedSet 380 Medium Map + Array
29 Sort By Frequency 451 Medium Freq + bucket
30 Max Subarray Sum K 325 Medium Prefix first index
Common Hash Table Patterns
1. Complement/Target Lookup
Store values, lookup complement. (Two Sum, 4Sum II)
2. Frequency Counting
Count occurrences, then process. (Top K, Majority, Sort by Freq)
3. Prefix Sum + Hash
{prefixSum: index/count} for subarray problems. (Subarray Sum K, Contiguous Array)
4. Sliding Window + Hash
Track elements in current window. (Longest Substring, Find Anagrams)
5. Cycle/Duplicate Detection
HashSet to detect repeats. (Happy Number, Contains Duplicate)
6. Object Cloning
Map original to clone for graph/list copying. (Clone Graph, Copy List)
7. Design Problems
Combine HashMap with other structures. (LRU Cache, RandomizedSet, Trie)
8/9
[Link] 2026-04-05
9/9