patterns
20 min
arrays
hashing

Pattern: Arrays & Hashing

Frequency maps, prefix sums, and the two-sum family.

Covers: arrays · hashing

When you smell this pattern - Need pairs/complements → hash map of seen values - Counts / anagrams → frequency map - Subarray sums → prefix sums + map of prefixes - “First unique” → count then scan

Template (complement) ``` map = {} for i, x in enumerate(nums): need = target - x if need in map: return [map[need], i] map[x] = i ```

Interview tip State why a nested loop fails the constraint **before** writing the map.

Practice Two Sum + Top K Frequent in the bank. Narrate the hash tradeoff.

Crucible — Interview prep with real proof