0
0
DSA Pythonprogramming~10 mins

Prefix Sum Array 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 prefix sum array with the first element of the input array.

DSA Python
prefix_sum = [[1]]
Drag options to blanks, or click blank then click option'
Aarr[0]
B0
C[]
Darr
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing prefix_sum as an empty list instead of starting with the first element.
Using 0 instead of the first element of the array.
2fill in blank
medium

Complete the code to append the sum of the last prefix sum and the current element to the prefix sum array.

DSA Python
for i in range(1, len(arr)):
    prefix_sum.append(prefix_sum[-1] [1] arr[i])
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.
Using the wrong operator causing incorrect prefix sums.
3fill in blank
hard

Fix the error in the code to correctly compute the prefix sum array.

DSA Python
prefix_sum = [arr[0]]
for i in range(1, len(arr)):
    prefix_sum.append(prefix_sum[i-1] [1] arr[i])
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.
Using integer division which changes the sum.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each element to its prefix sum if the element is greater than 0.

DSA Python
{word: [1] for word in arr if word [2] 0}
Drag options to blanks, or click blank then click option'
Aprefix_sum[arr.index(word)]
Bprefix_sum[word]
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as index causing errors.
Using wrong comparison operators.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps the uppercase of each key to its prefix sum value if the value is positive.

DSA Python
result = [1]: [2] for k, v in data.items() if v [3] 0}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase keys instead of uppercase.
Including values that are zero or negative.