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
What is the output of the following C# code that counts subarrays summing to k using a hash map?
DSA C#
using System; using System.Collections.Generic; class Program { static int SubarraySum(int[] nums, int k) { int count = 0, sum = 0; Dictionary<int, int> map = new Dictionary<int, int>(); map[0] = 1; foreach (int num in nums) { sum += num; if (map.ContainsKey(sum - k)) { count += map[sum - k]; } if (map.ContainsKey(sum)) { map[sum]++; } else { map[sum] = 1; } } return count; } static void Main() { int[] nums = {1, 2, 3}; int k = 3; Console.WriteLine(SubarraySum(nums, k)); } }
Attempts:
2 left
💡 Hint
Think about all continuous subarrays that sum to 3.
✗ Incorrect
The subarrays [1,2] and [3] both sum to 3, so the count is 2.
❓ Predict Output
intermediateResult of Subarray Sum with Negative Numbers
What is the output of this C# code counting subarrays summing to k when the array contains negative numbers?
DSA C#
using System; using System.Collections.Generic; class Program { static int SubarraySum(int[] nums, int k) { int count = 0, sum = 0; Dictionary<int, int> map = new Dictionary<int, int>(); map[0] = 1; foreach (int num in nums) { sum += num; if (map.ContainsKey(sum - k)) { count += map[sum - k]; } if (map.ContainsKey(sum)) { map[sum]++; } else { map[sum] = 1; } } return count; } static void Main() { int[] nums = {1, -1, 0}; int k = 0; Console.WriteLine(SubarraySum(nums, k)); } }
Attempts:
2 left
💡 Hint
Consider subarrays that sum exactly to zero.
✗ Incorrect
Subarrays [1,-1], [-1,0], and [0] sum to 0, so count is 3.
🔧 Debug
advancedIdentify the Runtime Error
What error does this C# code produce when counting subarrays summing to k?
DSA C#
using System; using System.Collections.Generic; class Program { static int SubarraySum(int[] nums, int k) { int count = 0, sum = 0; Dictionary<int, int> map = new Dictionary<int, int>(); // map[0] = 1; // This line is commented out foreach (int num in nums) { sum += num; if (map.ContainsKey(sum - k)) { count += map[sum - k]; } if (map.ContainsKey(sum)) { map[sum]++; } else { map[sum] = 1; } } return count; } static void Main() { int[] nums = {1, 2, 3}; int k = 3; Console.WriteLine(SubarraySum(nums, k)); } }
Attempts:
2 left
💡 Hint
Check if the dictionary contains keys before accessing them.
✗ Incorrect
Without initializing map[0] = 1, the code tries to access map[sum - k] when sum-k is 0, which is missing, causing KeyNotFoundException.
🧠 Conceptual
advancedWhy Use a Hash Map for Subarray Sum?
Why is a hash map used in the subarray sum equals k problem instead of a nested loop approach?
Attempts:
2 left
💡 Hint
Think about how prefix sums and counts help avoid repeated summations.
✗ Incorrect
The hash map stores counts of prefix sums to find how many subarrays sum to k in linear time, avoiding nested loops.
❓ Predict Output
expertOutput of Complex Subarray Sum Calculation
What is the output of this C# program that counts subarrays summing to k using a hash map with a larger input?
DSA C#
using System; using System.Collections.Generic; class Program { static int SubarraySum(int[] nums, int k) { int count = 0, sum = 0; Dictionary<int, int> map = new Dictionary<int, int>(); map[0] = 1; foreach (int num in nums) { sum += num; if (map.ContainsKey(sum - k)) { count += map[sum - k]; } if (map.ContainsKey(sum)) { map[sum]++; } else { map[sum] = 1; } } return count; } static void Main() { int[] nums = {3, 4, 7, 2, -3, 1, 4, 2}; int k = 7; Console.WriteLine(SubarraySum(nums, k)); } }
Attempts:
2 left
💡 Hint
Count all continuous subarrays that sum to 7 carefully.
✗ Incorrect
The subarrays summing to 7 are [7], [3,4], [7,2,-3,1], and [1,4,2], total 4.
