Why Hash Maps Are Your Best Friend
Hash maps (dictionaries) and sets are the most versatile data structures in coding interviews. They give you O(1) average time for insert, delete, and lookup operations.
Think of a hash map as a super-powered array where you can use ANY value as an index, not just integers 0 to n-1. Instead of array[5], you can do map['hello'] or map[someObject].
When to immediately think 'hash map':
- •'Find if X exists' -> Set for O(1) lookup
- •'Find pair that sums to target' -> Map to store complements
- •'Count occurrences' -> Map as frequency counter
- •'Group items by property' -> Map with computed key
- •'Check for duplicates' -> Set
The pattern recognition is crucial: whenever you see O(n²) nested loops checking 'have I seen this before?', a hash map can often reduce it to O(n).