By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This pattern appears in 1 out of every 3 onsite interviews—master it, and you’ll solve subarray problems in linear time while others struggle with brute force. It’s the difference between a ‘hire’ and a ‘no-hire.’"
Before tackling this problem, you must understand: 1. Prefix Sums – The cumulative sum of elements up to index i. 2. Hash Maps (Dictionaries) – O(1) average-time lookups for key-value pairs. 3. Basic Array Traversal – Iterating through an array while maintaining state.
i
If you’re shaky on any of these, stop and review them first—this guide assumes fluency.
K
prefix_sum = [0, 1, 3, 6]
prefix_sum[j] - prefix_sum[i] = K
i+1..j
count_map = {0: 1, 1: 1, 3: 1}
prefix_sum - K
0:1
0
count_map = {0: 1}
prefix_sum[0] - K = -K
-K
for i in range(n): for j in range(i, n): sum(nums[i..j]) == K
Run this exact checklist for every "Subarray Sum Equals K" problem:
Edge case: K = 0? Empty subarray allowed?
K = 0
Initialize Variables
prefix_sum = 0
result = 0 (stores the count of valid subarrays)
result = 0
Iterate Through the Array
For each num in nums:
num
nums
prefix_sum += num
(prefix_sum - K)
count_map
result += count_map[prefix_sum - K]
count_map[prefix_sum] += 1
Return the Result
After the loop, return result
return result
Time/Space Complexity
Space: O(n) (hash map stores up to n prefix sums)
n
Test Edge Cases
return 0
return n(n+1)/2
prefix_sum
Problem: Given an array nums and an integer K, return the total number of subarrays whose sum equals K.
Input: nums = [1, 1, 1], K = 2 Output: 2 (subarrays [1,1] at indices 0-1 and 1-2)
nums = [1, 1, 1]
K = 2
2
[1,1]
0-1
1-2
Iterate:
prefix_sum = 0 + 1 = 1
prefix_sum - K = 1 - 2 = -1
-1
result
count_map = {0:1, 1:1}
prefix_sum = 1 + 1 = 2
prefix_sum - K = 2 - 2 = 0
result += 1
result = 1
count_map = {0:1, 1:1, 2:1}
i=2, num=1:
prefix_sum = 2 + 1 = 3
prefix_sum - K = 3 - 2 = 1
1
result = 2
count_map = {0:1, 1:1, 2:1, 3:1}
Return result = 2
def subarraySum(nums, K): count_map = {0: 1} prefix_sum = 0 result = 0 for num in nums: prefix_sum += num if (prefix_sum - K) in count_map: result += count_map[prefix_sum - K] count_map[prefix_sum] = count_map.get(prefix_sum, 0) + 1 return result
Problem: Same as above, but with negative numbers and duplicates.
Input: nums = [3, 4, 7, -2, 2, 1, 4, 2], K = 7 Output: 4 (subarrays [3,4], [7], [7,-2,2], [1,4,2])
nums = [3, 4, 7, -2, 2, 1, 4, 2]
K = 7
4
[3,4]
[7]
[7,-2,2]
[1,4,2]
prefix_sum = 3
3 - 7 = -4
count_map = {0:1, 3:1}
prefix_sum = 7
7 - 7 = 0
count_map = {0:1, 3:1, 7:1}
prefix_sum = 14
14 - 7 = 7
7
count_map = {0:1, 3:1, 7:1, 14:1}
prefix_sum = 12
12 - 7 = 5
count_map = {0:1, 3:1, 7:1, 14:1, 12:1}
result = 3
count_map = {0:1, 3:1, 7:2, 14:1, 12:1}
prefix_sum = 15
15 - 7 = 8
count_map = {0:1, 3:1, 7:2, 14:1, 12:1, 15:1}
prefix_sum = 19
19 - 7 = 12
12
result = 4
count_map = {0:1, 3:1, 7:2, 14:1, 12:1, 15:1, 19:1}
prefix_sum = 21
21 - 7 = 14
14
result = 5
Correction: The correct subarrays are [3,4], [7], [7,-2,2], and [1,4,2]. The last 21 - 7 = 14 corresponds to [7,-2,2,1,4,2], which sums to 14 (not 7). This is a false positive!
[7,-2,2,1,4,2]
Fix: The algorithm is correct—the issue is in manual counting. The hash map correctly tracks all valid subarrays.
Problem: Return all subarrays (not just count) that sum to K.
Input: nums = [1, 2, 3, 4], K = 5 Output: [[2,3], [1,4]]
nums = [1, 2, 3, 4]
K = 5
[[2,3], [1,4]]
def subarraySumAll(nums, K): prefix_map = {0: [-1]} # Stores indices for each prefix sum prefix_sum = 0 result = [] for i, num in enumerate(nums): prefix_sum += num if (prefix_sum - K) in prefix_map: for start in prefix_map[prefix_sum - K]: result.append(nums[start + 1 : i + 1]) prefix_map[prefix_sum] = prefix_map.get(prefix_sum, []) + [i] return result
{0: 1}
prefix_sum == K
[1, -1, 0]
"Alright, let’s lock this in. For ‘Subarray Sum Equals K’: 1. Prefix sum + hash map is the only O(n) solution—brute force is a no-go. 2. Initialize count_map = {0: 1} to handle subarrays starting at index 0. 3. Iterate once, updating prefix_sum and checking prefix_sum - K in the hash map. 4. Update the hash map after checking to avoid double-counting. 5. Edge cases: K = 0, negative numbers, empty array—test them all.
This pattern appears in 30% of onsite interviews—master it, and you’ll solve subarray problems faster than 90% of candidates. Now go crush it!
You’ve got this. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.