Hashing & Maps — Complete Guide (Problems 161–205)

Sanjeev SharmaSanjeev Sharma
5 min read

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

PatternIdeaProblems
Complement MapStore seen → look up target-xTwo Sum, 4Sum II
Frequency MapCount occurrencesGroup Anagrams, Top K, Anagram check
Prefix + MapStore prefix sums → find rangeSubarray Sum = K
Two-Way MapBijection checkIsomorphic Strings, Word Pattern
Cycle DetectionHashSet visitedHappy Number
Index MapLast-seen / first-seen indexContains Duplicate II
LRU CacheHashMap + Doubly-Linked ListLRU Cache
LFU CacheTwo maps + min freq trackingLFU 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)

#ProblemPattern
161Two SumComplement Map
162Valid AnagramFrequency Map
163Ransom NoteFrequency Map
164Isomorphic StringsTwo-Way Map
165Word PatternTwo-Way Map
166Happy NumberHashSet Cycle
167Contains Duplicate IIIndex Map
168Find Common CharactersFreq Intersect
169Jewels and StonesHashSet
170Find Duplicate FileHashMap

🟡 Medium (171–200)

#ProblemPattern
171Group AnagramsSort Key Map
172Top K Frequent ElementsBucket Sort
173LRU CacheDLL + HashMap
174Subarray Sum Equals KPrefix + Map
175Continuous Subarray SumPrefix Mod Map
176Longest Consecutive SequenceHashSet O(n)
1774Sum IITwo-Sum Halves
178Insert Delete GetRandom O(1)Array + Map
179Random Pick with WeightPrefix + BS
180Find All Anagrams in StringSliding + Freq
181Brick WallGap Freq Map
182Unique Number of OccurrencesFreq + Set
183Number of Wonderful SubstringsBitmask + Map
184Equal Row and Column PairsRow Hash
185Two Sum IV (BST)HashSet BFS
186Longest Arithmetic SubsequenceDP + Map
187Count Number of Bad PairsMap Transform
188Make Sum Divisible by PPrefix Mod
189Tuple with Same ProductPair Product Map
190Encode and Decode TinyURLHashMap
191Fraction to Recurring DecimalRemainder Map
192Find Duplicate SubtreesSerialize + Map
193Design HashMapChaining
194Design HashSetChaining
195Random Pick IndexReservoir Sampling
196Most Common WordFreq Map
197Hand of StraightsSorted Freq Map
198Subarray Sum Divisible by KPrefix Mod
199Count Good MealsTwo Sum Pairs
200Time Based Key-Value StoreMap + Binary Search

🔴 Hard (201–205)

#ProblemPattern
201LFU CacheDouble Freq Map
202All O'one Data StructureDLL + Map
203Design TwitterHeap + Follow Map
204Palindrome PairsTrie/HashMap
205Hashing & Maps RecapAll Patterns

Next in Series

➡️ Linked Lists (Problems 206–260)

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro