O(1) range queries and subarray sum lookups using cumulative totals
220 problems·18 Easy·141 Medium·61 Hard
Pattern Study Guide & Cheat Sheet· O(n) single pass · O(n) for hash map / prefix array
▼
Precompute cumulative values so any range sum sum(nums[i..j]) is computed in O(1) time as prefix[j+1] - prefix[i], or combined with a Hash Map to find subarrays in O(N).
Core Invariant: Sum(i..j) = Prefix[j] - Prefix[i-1] = target implies Prefix[i-1] = Prefix[j] - target. By storing seen prefix sums in a Hash Map, we can count or find valid starting indices in O(1)!
Recognize it (Keywords & Signals)
Subarray sum equals K (especially with negative numbers)
Number of continuous subarrays divisible by K
Longest subarray with equal number of 0s and 1s
Repeated range sum queries on static array / 2D matrix
When NOT to use
Frequent updates/modifications to array elements (use Fenwick Tree / Segment Tree for O(log n) dynamic updates).
How to solve (Step-by-step)
1.Initialize `curr_sum = 0`, `count = 0`, and `seen = {0: 1}` (empty prefix has sum 0 occurring once).
2.Iterate through each number `x` in the array.
3.Add to running sum: `curr_sum += x`.
4.Check if `(curr_sum - target)` exists in `seen`. If so, add its frequency to `count`.
5.Record current sum in map: `seen[curr_sum] = seen.get(curr_sum, 0) + 1`.
6.Return `count`.
Watch for (Interview Traps)
Forgetting `{0: 1}` in the hash map (fails on valid subarrays starting from index 0)
Updating the hash map BEFORE checking `curr_sum - k` (can cause self-match when k = 0)
Mistakenly using Sliding Window when numbers can be negative (Sliding Window requires monotonic sums)
Prefix Sum + Hash Map (Subarray Sum Equals K)
def subarray_sum_equals_k(nums: list[int], k: int) -> int:
prefix_counts = {0: 1} # Base case: sum 0 occurs once before array starts
curr_sum = 0
total_subarrays = 0
for num in nums:
curr_sum += num
# If (curr_sum - k) was seen before, those prior points form valid subarrays
diff = curr_sum - k
if diff in prefix_counts:
total_subarrays += prefix_counts[diff]
prefix_counts[curr_sum] = prefix_counts.get(curr_sum, 0) + 1
return total_subarrays
Cost
O(n) single pass · O(n) for hash map / prefix array (Reduces an O(n^2) brute force subarray check down to O(n).)
Canonical problems
#560 Subarray Sum Equals K:Count subarrays with sum K even with negative numbers
#525 Contiguous Array:Map 0 -> -1; longest subarray with sum 0 has equal 0s and 1s
#238 Product of Array Except Self:Combine prefix products from left and suffix products from right
#304 Range Sum Query 2D - Immutable:2D prefix grid inclusion-exclusion area formula
What is the Prefix Sum & Hash Map pattern in coding interviews?
O(1) range queries and subarray sum lookups using cumulative totals It is categorized under Fundamentals and features 220 curated interview problems across Easy (18), Medium (141), and Hard (61) difficulties.
Which tech companies frequently test Prefix Sum & Hash Map?
Companies frequently asking Prefix Sum & Hash Map questions include Google, Amazon, Microsoft, Meta, Bloomberg, Infosys.
How do I master the Prefix Sum & Hash Map pattern?
Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.