0
0
DSA Pythonprogramming~20 mins

Kadane's Algorithm Maximum Subarray in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kadane's Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of Kadane's algorithm on this array?
Given the array nums = [-2,1,-3,4,-1,2,1,-5,4], what is the maximum subarray sum found by Kadane's algorithm?
DSA Python
def max_subarray(nums):
    max_current = max_global = nums[0]
    for num in nums[1:]:
        max_current = max(num, max_current + num)
        if max_current > max_global:
            max_global = max_current
    return max_global

nums = [-2,1,-3,4,-1,2,1,-5,4]
print(max_subarray(nums))
A6
B7
C5
D4
Attempts:
2 left
💡 Hint
Think about the contiguous subarray with the largest sum.
Predict Output
intermediate
2:00remaining
What is the maximum subarray sum for all negative numbers?
Using Kadane's algorithm, what is the maximum subarray sum for nums = [-8, -3, -6, -2, -5, -4]?
DSA Python
def max_subarray(nums):
    max_current = max_global = nums[0]
    for num in nums[1:]:
        max_current = max(num, max_current + num)
        if max_current > max_global:
            max_global = max_current
    return max_global

nums = [-8, -3, -6, -2, -5, -4]
print(max_subarray(nums))
A-5
B-2
C-4
D-3
Attempts:
2 left
💡 Hint
Kadane's algorithm returns the largest single element if all are negative.
🔧 Debug
advanced
2:00remaining
What error occurs in this Kadane's algorithm variant?
Consider this code variant of Kadane's algorithm. What error will it raise when run with nums = [-2, -1, -3]?
DSA Python
def max_subarray(nums):
    max_current = max_global = 0
    for num in nums:
        max_current = max(num, max_current + num)
        if max_current > max_global:
            max_global = max_current
    return max_global

nums = [-2, -1, -3]
print(max_subarray(nums))
AReturns 0 incorrectly
BReturns 4 incorrectly
CReturns 3 correctly
DRaises IndexError
Attempts:
2 left
💡 Hint
Check initial values and how they affect the result.
🧠 Conceptual
advanced
1:30remaining
What does Kadane's algorithm track during iteration?
During Kadane's algorithm execution, what does the variable max_current represent?
AThe sum of all elements in the array
BThe maximum sum of any subarray found so far
CThe maximum sum of a subarray ending at the current position
DThe minimum sum of a subarray ending at the current position
Attempts:
2 left
💡 Hint
Think about how the algorithm decides to continue or start a new subarray.
🚀 Application
expert
3:00remaining
Find the maximum subarray sum and the subarray itself
Modify Kadane's algorithm to return both the maximum subarray sum and the subarray elements for nums = [3, -2, 5, -1]. What is the correct output?
DSA Python
def max_subarray(nums):
    max_current = max_global = nums[0]
    start = end = s = 0
    for i in range(1, len(nums)):
        if nums[i] > max_current + nums[i]:
            max_current = nums[i]
            s = i
        else:
            max_current += nums[i]
        if max_current > max_global:
            max_global = max_current
            start = s
            end = i
    return max_global, nums[start:end+1]

nums = [3, -2, 5, -1]
print(max_subarray(nums))
A(7, [3, -2, 5, -1])
B(5, [5])
C(8, [3, -2, 5])
D(6, [3, -2, 5])
Attempts:
2 left
💡 Hint
Check how the algorithm updates start and end indices.