0
0
DSA Pythonprogramming~10 mins

Subarray Sum Equals K Using Hash Map in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the count of subarrays to zero.

DSA Python
count = [1]
Drag options to blanks, or click blank then click option'
ANone
B1
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting count from 1 or None instead of 0.
2fill in blank
medium

Complete the code to update the cumulative sum with the current number.

DSA Python
cumulative_sum [1] num
Drag options to blanks, or click blank then click option'
A+=
B/=
C*=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
3fill in blank
hard

Fix the error in checking if (cumulative_sum - k) exists in the hash map.

DSA Python
if (cumulative_sum [1] k) in prefix_sums:
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or multiplication instead of subtraction.
4fill in blank
hard

Fill both blanks to update the hash map with the current cumulative sum count.

DSA Python
prefix_sums[cumulative_sum] = prefix_sums.get([1], 0) [2] 1
Drag options to blanks, or click blank then click option'
Acumulative_sum
B+
C-
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key or subtracting instead of adding.
5fill in blank
hard

Fill all three blanks to complete the function that returns the count of subarrays summing to k.

DSA Python
def subarray_sum(nums, k):
    count = 0
    cumulative_sum = 0
    prefix_sums = [1]
    prefix_sums[0] = 1
    for num in nums:
        cumulative_sum [2] num
        if (cumulative_sum - k) in prefix_sums:
            count += prefix_sums[cumulative_sum - k]
        prefix_sums[cumulative_sum] = prefix_sums.get(cumulative_sum, 0) [3] 1
    return count
Drag options to blanks, or click blank then click option'
A{}
B+=
Cdefaultdict(int)
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using defaultdict without import, wrong operators, or wrong dictionary initialization.