Hashing & Maps — Complete Guide (Problems 161–205)
Advertisement
Series Introduction
Welcome to Hashing & Maps — the third chapter of the MAANG DSA Roadmap. HashMap and HashSet patterns power O(1) lookups that convert quadratic brute forces into linear solutions. This section covers 45 problems (#161–#205).
Previous: Two Pointers & Sliding Window (101–160)
Core HashMap Patterns
| Pattern | Idea | Problems |
|---|---|---|
| Complement Map | Store seen → look up target-x | Two Sum, 4Sum II |
| Frequency Map | Count occurrences | Group Anagrams, Top K, Anagram check |
| Prefix + Map | Store prefix sums → find range | Subarray Sum = K |
| Two-Way Map | Bijection check | Isomorphic Strings, Word Pattern |
| Cycle Detection | HashSet visited | Happy Number |
| Index Map | Last-seen / first-seen index | Contains Duplicate II |
| LRU Cache | HashMap + Doubly-Linked List | LRU Cache |
| LFU Cache | Two maps + min freq tracking | LFU Cache |
Key Templates
# Complement Map (Two Sum)
def two_sum(nums, target):
seen = {}
for i, x in enumerate(nums):
if target - x in seen: return [seen[target-x], i]
seen[x] = i
# Frequency Counter
from collections import Counter
def freq_map(s): return Counter(s)
# Prefix Sum + HashMap
from collections import defaultdict
def subarray_sum_k(nums, k):
prefix = cnt = 0
mp = defaultdict(int); mp[0] = 1
for n in nums:
prefix += n
cnt += mp[prefix - k]
mp[prefix] += 1
return cnt
# LRU Cache (OrderedDict)
from collections import OrderedDict
class LRUCache:
def __init__(self, cap):
self.cap = cap; self.cache = OrderedDict()
def get(self, key):
if key not in self.cache: return -1
self.cache.move_to_end(key); return self.cache[key]
def put(self, key, val):
if key in self.cache: self.cache.move_to_end(key)
self.cache[key] = val
if len(self.cache) > self.cap: self.cache.popitem(last=False)
Problem Index
🟢 Easy (161–170)
| # | Problem | Pattern |
|---|---|---|
| 161 | Two Sum | Complement Map |
| 162 | Valid Anagram | Frequency Map |
| 163 | Ransom Note | Frequency Map |
| 164 | Isomorphic Strings | Two-Way Map |
| 165 | Word Pattern | Two-Way Map |
| 166 | Happy Number | HashSet Cycle |
| 167 | Contains Duplicate II | Index Map |
| 168 | Find Common Characters | Freq Intersect |
| 169 | Jewels and Stones | HashSet |
| 170 | Find Duplicate File | HashMap |
🟡 Medium (171–200)
| # | Problem | Pattern |
|---|---|---|
| 171 | Group Anagrams | Sort Key Map |
| 172 | Top K Frequent Elements | Bucket Sort |
| 173 | LRU Cache | DLL + HashMap |
| 174 | Subarray Sum Equals K | Prefix + Map |
| 175 | Continuous Subarray Sum | Prefix Mod Map |
| 176 | Longest Consecutive Sequence | HashSet O(n) |
| 177 | 4Sum II | Two-Sum Halves |
| 178 | Insert Delete GetRandom O(1) | Array + Map |
| 179 | Random Pick with Weight | Prefix + BS |
| 180 | Find All Anagrams in String | Sliding + Freq |
| 181 | Brick Wall | Gap Freq Map |
| 182 | Unique Number of Occurrences | Freq + Set |
| 183 | Number of Wonderful Substrings | Bitmask + Map |
| 184 | Equal Row and Column Pairs | Row Hash |
| 185 | Two Sum IV (BST) | HashSet BFS |
| 186 | Longest Arithmetic Subsequence | DP + Map |
| 187 | Count Number of Bad Pairs | Map Transform |
| 188 | Make Sum Divisible by P | Prefix Mod |
| 189 | Tuple with Same Product | Pair Product Map |
| 190 | Encode and Decode TinyURL | HashMap |
| 191 | Fraction to Recurring Decimal | Remainder Map |
| 192 | Find Duplicate Subtrees | Serialize + Map |
| 193 | Design HashMap | Chaining |
| 194 | Design HashSet | Chaining |
| 195 | Random Pick Index | Reservoir Sampling |
| 196 | Most Common Word | Freq Map |
| 197 | Hand of Straights | Sorted Freq Map |
| 198 | Subarray Sum Divisible by K | Prefix Mod |
| 199 | Count Good Meals | Two Sum Pairs |
| 200 | Time Based Key-Value Store | Map + Binary Search |
🔴 Hard (201–205)
| # | Problem | Pattern |
|---|---|---|
| 201 | LFU Cache | Double Freq Map |
| 202 | All O'one Data Structure | DLL + Map |
| 203 | Design Twitter | Heap + Follow Map |
| 204 | Palindrome Pairs | Trie/HashMap |
| 205 | Hashing & Maps Recap | All Patterns |
Next in Series
Advertisement