Challenge - 5 Problems
Subarray Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Subarray Sum Count
What is the output of the following C code that counts subarrays summing to k using a hash map?
DSA C
#include <stdio.h> #include <stdlib.h> int subarraySum(int* nums, int numsSize, int k) { int count = 0, sum = 0; int *hash = calloc(2001, sizeof(int)); hash[1000] = 1; // offset for sum=0 for (int i = 0; i < numsSize; i++) { sum += nums[i]; if (sum - k + 1000 >= 0 && sum - k + 1000 <= 2000) { count += hash[sum - k + 1000]; } hash[sum + 1000]++; } free(hash); return count; } int main() { int nums[] = {1, 2, 3}; int k = 3; printf("%d\n", subarraySum(nums, 3, k)); return 0; }
Attempts:
2 left
💡 Hint
Think about all subarrays that sum to 3: [1,2] and [3].
✗ Incorrect
The subarrays that sum to 3 are [1,2] and [3], so the count is 2.
❓ Predict Output
intermediate2:00remaining
Result of Subarray Sum Count with Negative Numbers
What is the output of this C code counting subarrays summing to k with negative numbers?
DSA C
#include <stdio.h> #include <stdlib.h> int subarraySum(int* nums, int numsSize, int k) { int count = 0, sum = 0; int *hash = calloc(4001, sizeof(int)); hash[2000] = 1; // offset for sum=0 for (int i = 0; i < numsSize; i++) { sum += nums[i]; if (sum - k + 2000 >= 0 && sum - k + 2000 <= 4000) { count += hash[sum - k + 2000]; } hash[sum + 2000]++; } free(hash); return count; } int main() { int nums[] = {1, -1, 0}; int k = 0; printf("%d\n", subarraySum(nums, 3, k)); return 0; }
Attempts:
2 left
💡 Hint
Count all subarrays that sum to zero including single elements and combinations.
✗ Incorrect
Subarrays summing to 0 are: [1,-1], [-1,0], [0], so total 3.
🔧 Debug
advanced2:00remaining
Identify the Runtime Error in Subarray Sum Code
Which option will cause a runtime error when running this subarray sum code?
DSA C
#include <stdio.h> #include <stdlib.h> int subarraySum(int* nums, int numsSize, int k) { int count = 0, sum = 0; int *hash = calloc(1000, sizeof(int)); hash[500] = 1; for (int i = 0; i < numsSize; i++) { sum += nums[i]; count += hash[sum - k + 500]; hash[sum + 500]++; } free(hash); return count; } int main() { int nums[] = {1, 2, 3}; int k = 3; printf("%d\n", subarraySum(nums, 3, k)); return 0; }
Attempts:
2 left
💡 Hint
Check array index calculations for negative values.
✗ Incorrect
sum-k+500 can be negative causing invalid array access and runtime error.
🧠 Conceptual
advanced2:00remaining
Why Use a Hash Map for Subarray Sum Equals K?
Why is a hash map used in the subarray sum equals k problem instead of nested loops?
Attempts:
2 left
💡 Hint
Think about how prefix sums and counts help find subarrays quickly.
✗ Incorrect
Hash map stores counts of prefix sums to find subarrays in linear time.
🚀 Application
expert2:00remaining
Find Number of Subarrays Summing to k in Large Array
Given an array of 10,000 elements all equal to 1 and k = 3, what is the number of subarrays that sum to k?
Attempts:
2 left
💡 Hint
Count how many continuous subarrays of length 3 exist in the array.
✗ Incorrect
Each subarray of length 3 sums to 3. Number of such subarrays = n - k + 1 = 10000 - 3 + 1 = 9998.
