Pattern #4
Hashing / Hash Map / Hash Set
Important interview questions, thinking patterns, production use cases, and Go tips for hashing problems.
Must Solve
17 core questions — solve these first.
- 1.Two Sumvalue → indexeasy
- 2.Contains DuplicateHash Seteasy
- 3.Valid AnagramFrequency Arrayeasy
- 4.Group AnagramsSort key → listmedium
- 5.Intersection of Two ArraysHash Seteasy
- 6.First Unique Character in a StringFrequency Mapeasy
- 7.Ransom NoteFrequency Mapeasy
- 8.Isomorphic StringsTwo Mapseasy
- 9.Word PatternTwo Mapseasy
- 10.Happy NumberHash Set cycleeasy
- 11.Longest Consecutive SequenceHash Setmedium
- 12.Top K Frequent ElementsMap + Bucketmedium
- 13.Subarray Sum Equals KPrefix Sum + Mapmedium
- 14.Longest Substring Without Repeating CharactersSliding Window + Mapmedium
- 15.Find All Anagrams in a StringSliding Window + Mapmedium
- 16.4Sum IITwo Mapsmedium
- 17.Minimum Window SubstringSliding Window + Maphard
Also Important
9 more questions worth practicing.
- 18.Majority ElementBoyer-Moore / Mapeasy
- 19.Sort Characters By FrequencyMap + Bucketmedium
- 20.Find Common CharactersFreq intersectioneasy
- 21.Unique Number of OccurrencesMap + Seteasy
- 22.Check if N and Its Double ExistHash Seteasy
- 23.Contains Duplicate IIMap indexeasy
- 24.Encode and Decode StringsLength prefixmedium
- 25.Design HashMapChaining / Arrayeasy
- 26.LRU CacheMap + Doubly LLmedium
How to Think
- Need fast lookup?Hash Map
- Need only know if something exists?Hash Set
- Need frequency/count?value → count
- Need remember previous index?value → index
- Need remove duplicates?Hash Set
- Need group items?key → list
- Need pair with target?target - current, check Map
- Need longest consecutive numbers?Hash Set (avoid sorting)
- Need subarray sum = K?Prefix Sum + Hash Map
Small Rules
Map
Store information about something.
userID → user
number → count
value → indexSet
Store only whether something exists.
seen[value]Complexity
Average:
Lookup → O(1)
Insert → O(1)
Delete → O(1)
Extra space usually: O(n)
Production Thinking
Fast lookup
Instead of scanning 1 million users:
Loop → O(n)
Use: userID → user → O(1)
Remove duplicate events
A server receives the same message twice.
processedMessageIDsIf ID already exists → ignore it.
Count things
productID → numberOfOrdersVery common in real systems.
Cache data
userID → cachedUserAvoid hitting the database every time.
More speed often means more memory.
Array scan → less memory, slower lookup
Hash Map → more memory, faster lookup
Go Tips
Map
m := make(map[int]int)Set
seen := make(map[int]bool)Check existence
value, ok := m[key]Go map order is not guaranteed. Do not depend on map iteration order.
Normal Go maps are not safe for concurrent writes. Production code may need locking or sync.Map.
Interview Rules
- Need lookup?Map
- Need uniqueness?Set
- Need frequency?Map
- Need value + index?Map
- Need pair target?Store previous values
- Need duplicates?Set
- Need grouping?Map of lists
💡 Hashing often changes O(n²) into O(n).
Remember This
Exists? → Set
Count? → Map
Index? → Map
Group? → Map
Duplicate? → Set
Fast lookup? → Hashing
Pair target? → Map
Prefix sum? → Map