Bird
0
0
DSA Cprogramming~20 mins

Subarray Sum Equals K Using Hash Map in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subarray Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A0
B1
C2
D3
Attempts:
2 left
💡 Hint
Think about all subarrays that sum to 3: [1,2] and [3].
Predict Output
intermediate
2: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;
}
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Count all subarrays that sum to zero including single elements and combinations.
🔧 Debug
advanced
2: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;
}
AAccessing hash at negative index causes runtime error
BMemory leak error
CDivision by zero error occurs
DNo runtime error, code runs fine
Attempts:
2 left
💡 Hint
Check array index calculations for negative values.
🧠 Conceptual
advanced
2: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?
ATo reduce time complexity from O(n^2) to O(n) by storing prefix sums counts
BTo save memory by storing only unique elements
CTo sort the array before processing
DTo avoid using recursion
Attempts:
2 left
💡 Hint
Think about how prefix sums and counts help find subarrays quickly.
🚀 Application
expert
2: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?
A9999
B9997
C10000
D9998
Attempts:
2 left
💡 Hint
Count how many continuous subarrays of length 3 exist in the array.