CONTENTS

Hashing and Prefix Sum Patterns Explained

Learn when to use hash maps, hash sets, frequency arrays, and prefix sums in coding interviews.

May 29, 2026
44
A

Hashing is the pattern you reach for when brute force keeps asking, "Have I seen the matching value before?" Prefix sum is the version for subarrays, where the matching value is a previous running sum.

Pattern Table

SignalUseLesson
Find complementHash map value to indexTwo Sum
Count repeated symbolsFrequency mapValid Anagram
Longest consecutive runHash set sequence startsLongest Consecutive Sequence
Subarray target sumPrefix sum frequencySubarray Sum Equals K

Core Formula

currentPrefix - previousPrefix = target
previousPrefix = currentPrefix - target

FAQs

Why not use sliding window for every subarray sum?

Sliding window needs monotonic movement. Negative numbers break that, while prefix sums still work.

What is the most common hashing mistake?

Updating the map before checking the current value when the problem requires earlier elements only.

Share this article

0 comments

Please login to comment.
No comments yet.