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 TypeScript code that counts subarrays summing to k?
DSA Typescript
function subarraySum(nums: number[], k: number): number {
let count = 0;
let sum = 0;
const map = new Map<number, number>();
map.set(0, 1);
for (const num of nums) {
sum += num;
if (map.has(sum - k)) {
count += map.get(sum - k)!;
}
map.set(sum, (map.get(sum) || 0) + 1);
}
return count;
}
console.log(subarraySum([1, 2, 3, -1, 2], 3));Attempts:
2 left
💡 Hint
Think about how many continuous parts of the array add up to 3.
✗ Incorrect
The subarrays that sum to 3 are: [1, 2], [3]. So total count is 2.
❓ Predict Output
intermediateResult of Subarray Sum with Negative Numbers
What does this code print when counting subarrays summing to k = 0?
DSA Typescript
function subarraySum(nums: number[], k: number): number {
let count = 0;
let sum = 0;
const map = new Map<number, number>();
map.set(0, 1);
for (const num of nums) {
sum += num;
if (map.has(sum - k)) {
count += map.get(sum - k)!;
}
map.set(sum, (map.get(sum) || 0) + 1);
}
return count;
}
console.log(subarraySum([1, -1, 1, -1], 0));Attempts:
2 left
💡 Hint
Count all continuous parts that sum to zero.
✗ Incorrect
Subarrays summing to 0 are: [1, -1], [-1, 1], [1, -1], [1, -1, 1, -1]. Total 4.
🔧 Debug
advancedIdentify the Error in Subarray Sum Code
What error will this TypeScript code produce when run?
DSA Typescript
function subarraySum(nums: number[], k: number): number {
let count = 0;
let sum = 0;
const map = new Map<number, number>();
for (const num of nums) {
sum += num;
if (map.has(sum - k)) {
count += map.get(sum - k)!;
}
map.set(sum, (map.get(sum) || 0) + 1);
}
return count;
}
console.log(subarraySum([1, 2, 3], 3));Attempts:
2 left
💡 Hint
Check if the map is initialized correctly before the loop.
✗ Incorrect
The map does not have an initial entry for sum 0, so it misses counting subarrays starting from the beginning ([1,2]), outputting 1 instead of 2. No runtime error.
🧠 Conceptual
advancedWhy 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 sum k.
✗ Incorrect
The hash map stores how many times each prefix sum has appeared, enabling O(1) lookup to find if a subarray sums to k.
❓ Predict Output
expertOutput of Subarray Sum with Large Input
What is the output of this code counting subarrays summing to 5?
DSA Typescript
function subarraySum(nums: number[], k: number): number {
let count = 0;
let sum = 0;
const map = new Map<number, number>();
map.set(0, 1);
for (const num of nums) {
sum += num;
if (map.has(sum - k)) {
count += map.get(sum - k)!;
}
map.set(sum, (map.get(sum) || 0) + 1);
}
return count;
}
const arr = [1, 2, 3, 2, 1, 2, 3, 2, 1];
console.log(subarraySum(arr, 5));Attempts:
2 left
💡 Hint
Count all continuous subarrays that sum exactly to 5.
✗ Incorrect
The subarrays summing to 5 are: [2,3] (1-2), [3,2] (2-3), [2,1,2] (3-5), [2,3] (5-6), [3,2] (6-7). Total 5.
