foundation
Hash map task patterns
Use maps for complements, grouping, frequency, first-seen indexes, and memoization.
Hash maps appear when the question asks whether you have seen a value, how often it appears, where it appeared first, or which group it belongs to. They turn repeated scans into indexed lookups at the cost of extra memory and careful key design.
On interviews: Common variants are two-sum complements, anagram grouping, longest substring with counts, memoizing recursive results, and deduplicating graph visits.
Common pitfalls: Using objects as keys in plain objects coerces to strings. JSON stringified composite keys can collide if not designed carefully. Memory can be larger than the input when keys store slices.
Checklist:
- Choose key representation.
- Store count, index, or payload deliberately.
- Count memory.
- Clean up when using a moving window.