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 Java code that counts subarrays summing to k=5?
DSA Java
import java.util.HashMap; public class Solution { public static int subarraySum(int[] nums, int k) { int count = 0, sum = 0; HashMap<Integer, Integer> map = new HashMap<>(); map.put(0, 1); for (int num : nums) { sum += num; if (map.containsKey(sum - k)) { count += map.get(sum - k); } map.put(sum, map.getOrDefault(sum, 0) + 1); } return count; } public static void main(String[] args) { int[] nums = {1, 2, 3, 2, 1}; System.out.println(subarraySum(nums, 5)); } }
Attempts:
2 left
💡 Hint
Count all continuous subarrays whose sum equals 5.
✗ Incorrect
The subarrays that sum to 5 are [2,3] (indices 1-2), [3,2] (indices 2-3), and [2,3,2,1] (indices 1-4). The code correctly counts 3 such subarrays.
🧠 Conceptual
intermediateWhy use a HashMap in Subarray Sum Equals K?
Why do we use a HashMap to solve the Subarray Sum Equals K problem efficiently?
Attempts:
2 left
💡 Hint
Think about how prefix sums help find subarrays quickly.
✗ Incorrect
The HashMap stores how many times each prefix sum has appeared. This helps quickly find if a subarray sum equals k by checking if (current_sum - k) exists.
🔧 Debug
advancedIdentify the error in this Subarray Sum code
What is the output of this Java code when run?
DSA Java
import java.util.HashMap; public class Solution { public static int subarraySum(int[] nums, int k) { int count = 0, sum = 0; HashMap<Integer, Integer> map = new HashMap<>(); for (int num : nums) { sum += num; if (map.containsKey(sum - k)) { count += map.get(sum - k); } map.put(sum, map.getOrDefault(sum, 0) + 1); } return count; } public static void main(String[] args) { int[] nums = {1, 2, 3}; System.out.println(subarraySum(nums, 3)); } }
Attempts:
2 left
💡 Hint
Check if the map is initialized with prefix sum 0.
✗ Incorrect
The map does not have an initial entry for prefix sum 0, so when sum-k equals 0 it skips counting subarrays from the start. Outputs 1 (only [3]), missing [1,2]. No exception.
❓ Predict Output
advancedOutput of Subarray Sum Count with negative numbers
What is the output of this Java code counting subarrays summing to k=0 with negative numbers?
DSA Java
import java.util.HashMap; public class Solution { public static int subarraySum(int[] nums, int k) { int count = 0, sum = 0; HashMap<Integer, Integer> map = new HashMap<>(); map.put(0, 1); for (int num : nums) { sum += num; if (map.containsKey(sum - k)) { count += map.get(sum - k); } map.put(sum, map.getOrDefault(sum, 0) + 1); } return count; } public static void main(String[] args) { int[] nums = {1, -1, 0}; System.out.println(subarraySum(nums, 0)); } }
Attempts:
2 left
💡 Hint
Count all subarrays that sum to zero including single elements.
✗ Incorrect
The subarrays summing to 0 are [1, -1], [1, -1, 0], [0]. The code counts 3 such subarrays.
🚀 Application
expertMaximum number of subarrays summing to k in large array
Given an array of 10^6 elements all equal to 1, and k=2, how many subarrays sum to k?
Attempts:
2 left
💡 Hint
Count how many pairs of consecutive 1s exist in the array.
✗ Incorrect
Each subarray of length 2 sums to 2. There are (n-1) such subarrays in an array of length n=10^6, so 999999.
