What is HashMap?
HashMap (or HashTable/Dictionary) provides O(1) average-case lookup, insertion, and deletion by mapping keys to values using a hash function.Quick Recognition: If you need O(1) lookup, frequency counting, or want to trade space for time, HashMap is your friend!
Pattern Recognition Checklist
Use HashMap When
- Need O(1) lookup by key
- Counting frequencies of elements
- Finding pairs/complements (like Two Sum)
- Grouping elements by property
- Caching/Memoization
Don't Use When
- Need sorted order (use TreeMap)
- Range queries required
- Space is very constrained
- Keys are unhashable (like lists)
When to Use
Frequency Counting
Count occurrences of elements
Two Sum Variants
Find pairs/groups with specific properties
Caching/Memoization
Store computed results
Grouping/Indexing
Group elements by property
Pattern Variations
1. Two Sum (Classic HashMap)
2. Frequency Counter
3. Group By (Anagrams)
4. Prefix Sum with HashMap
5. Longest Consecutive Sequence
6. LRU Cache (HashMap + Doubly Linked List)
Classic Problems
1. Two Sum
1. Two Sum
Pattern: Store complement, check on each iterationKey: HashMap trades space for time O(n^2) to O(n)
2. Valid Anagram
2. Valid Anagram
Pattern: Compare character frequency countsVariants: Counting sort (26 letters), Counter equality
3. Contains Duplicate
3. Contains Duplicate
Pattern: HashSet for seen elementsVariants: Within k indices, within t value difference
4. First Unique Character
4. First Unique Character
Pattern: Count frequency, find first with count 1Key: Two passes - count, then find
5. Subarray Sum Equals K
5. Subarray Sum Equals K
Pattern: Prefix sum with HashMapKey: Store prefix_sum frequencies, look for (current - k)
Common Mistakes
Complexity Quick Reference
| Operation | Average | Worst Case | Note |
|---|---|---|---|
| Insert | O(1) | O(n) | Worst case with bad hash |
| Lookup | O(1) | O(n) | Worst case with collisions |
| Delete | O(1) | O(n) | Worst case with chaining |
| Iterate | O(n) | O(n) | All keys/values |
Interview Problems by Company
- Easy
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Two Sum | All FAANG | Complement lookup |
| Valid Anagram | Amazon, Meta | Character frequency |
| Contains Duplicate | All | HashSet for seen |
| First Unique Char | Amazon | Frequency count |
| Ransom Note | Microsoft | Character counting |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “I’ll use a HashMap to get O(1) lookup time.”
- “I’ll store [key] mapping to [value].”
- “This trades O(n) space for O(1) lookup time.”
- “For each element, I check if [complement/target] exists.”
- “Total time is O(n) with O(n) space.”
When Interviewer Says...
When Interviewer Says...
| Interviewer Says | You Should Think |
|---|---|
| ”Can you do it in O(n)?” | HashMap to avoid nested loop |
| ”Count occurrences” | Frequency counter |
| ”Find pair that sums to…” | Store complements |
| ”Group by property” | Key = property, value = list |
| ”Check if seen before” | HashSet |
HashMap vs HashSet
HashMap vs HashSet
HashMap: When you need key-value pairs
- Two Sum (value → index)
- Frequency counting (element → count)
- Contains Duplicate (seen set)
- Unique elements (deduplication)
Practice Problems
| Problem | Difficulty | Link |
|---|---|---|
| Two Sum | Easy | LeetCode 1 |
| Valid Anagram | Easy | LeetCode 242 |
| Group Anagrams | Medium | LeetCode 49 |
| Subarray Sum Equals K | Medium | LeetCode 560 |
| LRU Cache | Medium | LeetCode 146 |
Practice Roadmap
1
Day 1: Basic Lookup
- Solve: Two Sum, Contains Duplicate
- Focus: Complement pattern, seen set
2
Day 2: Frequency Counting
- Solve: Valid Anagram, First Unique Character
- Focus: Counter/frequency map usage
3
Day 3: Grouping
- Solve: Group Anagrams, Word Pattern
- Focus: Key design for grouping
4
Day 4: Advanced
- Solve: LRU Cache, Subarray Sum = K
- Focus: HashMap + other data structures