Common Hashing Interview Patterns
Most hashing interview problems reduce a scan from quadratic to linear by storing exactly the past information needed to answer the current question.
The Core Pattern
Hashing is most powerful when the brute-force solution repeatedly asks the same lookup question. Instead of scanning all previous items for each current item, store a searchable summary of the past. Then each new element can ask the hash table for the one fact it needs.
The mental move is: what would I search for in the left side if I were doing the slow solution? That searched-for thing often becomes the hash key. The attached value, if any, is whatever information the future needs after the match is found.
Complement Lookup
The signal for complement lookup is a target relationship between two values. In Two Sum, when the current number is x, the missing earlier value is target - x. A map from value to index lets the scan answer whether that complement already appeared.
The ordering matters. Usually you check for the complement before inserting the current value so an element is not paired with itself. This pattern also appears in pair counts, fixed-difference questions, and problems where the current item determines exactly which previous key would complete the answer.
Frequency, Membership, and Dedup
Frequency counting is the right signal when the problem asks for most common, exactly once, same multiset, or how many occurrences. The key is the item being counted, and the value is its count. Valid Anagram, Top K Frequent Elements, and many voting-style array tasks start with this move.
Membership and deduplication use a set when counts do not matter. The signal is any phrasing like already seen, exists, unique, duplicate, or visited. A set is also useful as a precomputation step: load all values, then test candidate neighbors or complements in expected O(1) time.
Canonical Grouping and Prefix Sums
Grouping by a canonical key appears when different raw inputs should be treated as equivalent. Group Anagrams converts each word into a canonical representation, then maps that representation to all words with the same signature. The key design is the whole problem: sorted letters, character counts, or another stable signature.
Prefix-sum plus hashmap appears when the question asks about subarrays with a target sum. If the current prefix is sum, then a previous prefix sum - k marks a subarray ending here with sum k. The map stores how many times each previous prefix has occurred, not just whether it occurred, because multiple starts can lead to multiple valid subarrays.
Caching and Memoization
Caching is the hashing pattern for repeated expensive questions. The signal is that the same input state or computed key can be requested more than once. A map from state to answer turns repeated work into a lookup.
This can appear inside dynamic programming, graph search, string processing, and design problems. In arrays and hashing interviews, it often shows up as memoizing normalized forms, caching expensive transformations, or building an LRU-style structure where HashMap lookup is paired with another structure that maintains order.
Complement lookup for Two Sum
Each step asks whether the exact value needed to pair with the current number has already appeared.
Key Takeaways
- Use complement lookup when the current item determines the exact previous key that would complete a target relationship.
- Use frequency maps for counts and multisets; use sets for membership, deduplication, and visited checks.
- Use canonical keys when different inputs should be grouped as equivalent, such as anagrams.
- Use prefix-sum maps and caches when the answer depends on repeated historical states rather than only raw elements.