Challenge - 5 Problems
Subarray Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of subarray sum count with k=5
What is the output of the following Go code that counts subarrays summing to k=5?
DSA Go
package main import "fmt" func subarraySum(nums []int, k int) int { count := 0 sum := 0 prefixSums := map[int]int{0: 1} for _, num := range nums { sum += num if v, ok := prefixSums[sum-k]; ok { count += v } prefixSums[sum]++ } return count } func main() { nums := []int{1, 2, 3, 2, 1} k := 5 fmt.Println(subarraySum(nums, k)) }
Attempts:
2 left
💡 Hint
Think about all continuous subarrays that sum exactly to 5.
✗ Incorrect
The subarrays that sum to 5 are [2,3] (indices 1-2) and [3,2] (indices 2-3). The code correctly counts 2 such subarrays.
🧠 Conceptual
intermediateWhy use a hash map in subarray sum problem?
Why is a hash map used in the subarray sum equals k problem?
Attempts:
2 left
💡 Hint
Think about how to find if a previous sum exists that helps form k.
✗ Incorrect
The hash map stores how many times each prefix sum has appeared. This allows quick checks if sum-k exists, indicating a subarray summing to k.
🔧 Debug
advancedIdentify the error in subarray sum code snippet
What error will this Go code produce when counting subarrays summing to k=3?
DSA Go
package main import "fmt" func subarraySum(nums []int, k int) int { count := 0 sum := 0 prefixSums := map[int]int{} for _, num := range nums { sum += num if v, ok := prefixSums[sum-k]; ok { count += v } prefixSums[sum]++ } return count } func main() { nums := []int{1, 2, 1, 2, 1} k := 3 fmt.Println(subarraySum(nums, k)) }
Attempts:
2 left
💡 Hint
Check how prefixSums map is initialized.
✗ Incorrect
The map prefixSums is initialized empty without the base case 0:1, so subarrays starting at index 0 are not counted. For nums=[1,2,1,2,1], k=3, there are 4 subarrays summing to 3, but the code returns 3.
❓ Predict Output
advancedOutput of subarray sum count with negative numbers
What is the output of this Go code counting subarrays summing to k=0 with negative numbers?
DSA Go
package main import "fmt" func subarraySum(nums []int, k int) int { count := 0 sum := 0 prefixSums := map[int]int{0: 1} for _, num := range nums { sum += num if v, ok := prefixSums[sum-k]; ok { count += v } prefixSums[sum]++ } return count } func main() { nums := []int{1, -1, 0, 0} k := 0 fmt.Println(subarraySum(nums, k)) }
Attempts:
2 left
💡 Hint
Consider subarrays including zeros and negative numbers.
✗ Incorrect
The subarrays summing to 0 are [1,-1], [1,-1,0], [1,-1,0,0], [0] (index 2), [0] (index 3), [0,0] (indices 2-3). There are 6 such subarrays.
🚀 Application
expertNumber of subarrays summing to k in large input
Given an array of 100000 elements all equal to 1, and k=2, how many subarrays sum to k?
Attempts:
2 left
💡 Hint
Count how many continuous pairs sum to 2 in a large array of ones.
✗ Incorrect
Each pair of consecutive 1s sums to 2. There are n-1 such pairs in an array of length n=100000.
