0
0
DSA Pythonprogramming~10 mins

Kadane's Algorithm Maximum Subarray 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 variable that keeps track of the current subarray sum.

DSA Python
def max_subarray_sum(nums):
    current_sum = [1]
    max_sum = nums[0]
    for num in nums[1:]:
        current_sum = max(num, current_sum + num)
        max_sum = max(max_sum, current_sum)
    return max_sum
Drag options to blanks, or click blank then click option'
A0
B1
Cfloat('-inf')
Dnums[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing current_sum to 0 causes incorrect results when all numbers are negative.
2fill in blank
medium

Complete the code to update the current sum by choosing the maximum between the current number and the sum including it.

DSA Python
def max_subarray_sum(nums):
    current_sum = nums[0]
    max_sum = nums[0]
    for num in nums[1:]:
        current_sum = [1]
        max_sum = max(max_sum, current_sum)
    return max_sum
Drag options to blanks, or click blank then click option'
Amax(num, current_sum + num)
Bnum + current_sum
Cmin(num, current_sum + num)
Dcurrent_sum - num
Attempts:
3 left
💡 Hint
Common Mistakes
Using min instead of max causes wrong subarray sums.
Not including the current number in the sum.
3fill in blank
hard

Fix the error in the loop to correctly iterate over the array starting from the second element.

DSA Python
def max_subarray_sum(nums):
    current_sum = nums[0]
    max_sum = nums[0]
    for num in nums[1]:
        current_sum = max(num, current_sum + num)
        max_sum = max(max_sum, current_sum)
    return max_sum
Drag options to blanks, or click blank then click option'
A[0:]
B[:1]
C[1:]
D[2:]
Attempts:
3 left
💡 Hint
Common Mistakes
Using [:1] iterates only the first element.
Using [0:] includes the first element again causing double counting.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each number to its square if the number is positive.

DSA Python
squares = {num: num[1]2 for num in nums if num [2] 0}
Drag options to blanks, or click blank then click option'
A**
B>
C<
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '**' for squares.
Using '<' instead of '>' for filtering positive numbers.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 3.

DSA Python
result = { [1]: [2] for word in words if len(word) [3] 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Mapping word instead of word.upper().