Unit 10 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
nums
is empty?
k
.HAPPY CASE
Input: nums = [1, 1, 1], k = 2
Output: 2
Explanation: The subarrays [1, 1] (starting at index 0) and [1, 1] (starting at index 1) sum to 2.
HAPPY CASE
Input: nums = [1, 2, 3], k = 3
Output: 2
Explanation: The subarrays [1, 2] and [3] sum to 3.
EDGE CASE
Input: nums = [], k = 0
Output: 0
Explanation: The array is empty, so the output is 0.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Array problems involving subarrays and sums, we want to consider the following approaches:
k
.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use a hash map to store the cumulative sums and their frequencies. Iterate through the array, maintaining the current cumulative sum. For each element, check if the cumulative sum minus k
exists in the hash map. If it does, add the frequency of this sum to the count. Update the hash map with the current cumulative sum.
1) Initialize a hash map `cumulative_sum` with a default value of 0 for the cumulative sum 0.
2) Initialize `current_sum` to 0 and `count` to 0.
3) Iterate through each element `num` in the array `nums`:
a) Add `num` to `current_sum`.
b) If `current_sum` is equal to `k`, increment `count`.
c) If `current_sum - k` exists in `cumulative_sum`, add its frequency to `count`.
d) Update `cumulative_sum` with `current_sum`.
4) Return `count`.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def subarray_sum(nums, k):
cumulative_sum = {0: 1}
current_sum = 0
count = 0
for num in nums:
current_sum += num
if current_sum == k:
count += 1
if current_sum - k in cumulative_sum:
count += cumulative_sum[current_sum - k]
if current_sum in cumulative_sum:
cumulative_sum[current_sum] += 1
else:
cumulative_sum[current_sum] = 1
return count
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the length of the array nums
.
O(N)
because we need to iterate through the array once.O(N)
because we need to store the cumulative sums in a hash map.