Challenge - 5 Problems
Kadane's Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of Kadane's algorithm on this array?
Given the array [-2, 1, -3, 4, -1, 2, 1, -5, 4], what is the maximum subarray sum found by Kadane's algorithm?
DSA Go
func maxSubArray(nums []int) int { maxSoFar := nums[0] maxEndingHere := 0 for _, num := range nums { maxEndingHere += num if maxEndingHere > maxSoFar { maxSoFar = maxEndingHere } if maxEndingHere < 0 { maxEndingHere = 0 } } return maxSoFar } func main() { nums := []int{-2, 1, -3, 4, -1, 2, 1, -5, 4} println(maxSubArray(nums)) }
Attempts:
2 left
💡 Hint
Think about the largest sum of a continuous subarray.
✗ Incorrect
Kadane's algorithm finds the maximum sum of a contiguous subarray. Here, the subarray [4, -1, 2, 1] sums to 6, which is the maximum.
❓ Predict Output
intermediateWhat is the maximum subarray sum for all negative numbers?
Given the array [-8, -3, -6, -2, -5, -4], what is the maximum subarray sum Kadane's algorithm returns?
DSA Go
func maxSubArray(nums []int) int { maxSoFar := nums[0] maxEndingHere := 0 for _, num := range nums { maxEndingHere += num if maxEndingHere > maxSoFar { maxSoFar = maxEndingHere } if maxEndingHere < 0 { maxEndingHere = 0 } } return maxSoFar } func main() { nums := []int{-8, -3, -6, -2, -5, -4} println(maxSubArray(nums)) }
Attempts:
2 left
💡 Hint
Kadane's algorithm resets sum to zero when negative, but initial max is first element.
✗ Incorrect
When all numbers are negative, Kadane's algorithm returns the largest single element, which is -2 here.
🔧 Debug
advancedWhy does this Kadane's algorithm implementation fail on all negative input?
Consider this code snippet for Kadane's algorithm. Why does it return 0 instead of the correct maximum subarray sum for all negative numbers?
func maxSubArray(nums []int) int {
maxSoFar := 0
maxEndingHere := 0
for _, num := range nums {
maxEndingHere += num
if maxEndingHere > maxSoFar {
maxSoFar = maxEndingHere
}
if maxEndingHere < 0 {
maxEndingHere = 0
}
}
return maxSoFar
}
DSA Go
func maxSubArray(nums []int) int { maxSoFar := 0 maxEndingHere := 0 for _, num := range nums { maxEndingHere += num if maxEndingHere > maxSoFar { maxSoFar = maxEndingHere } if maxEndingHere < 0 { maxEndingHere = 0 } } return maxSoFar }
Attempts:
2 left
💡 Hint
Think about the initial value of maxSoFar and how it affects negative inputs.
✗ Incorrect
Initializing maxSoFar to 0 causes the function to return 0 even when all numbers are negative, which is incorrect. It should start with the first element of the array.
🧠 Conceptual
advancedWhat is the time complexity of Kadane's algorithm?
Kadane's algorithm finds the maximum sum of a contiguous subarray. What is its time complexity in terms of the input array size n?
Attempts:
2 left
💡 Hint
Kadane's algorithm scans the array once.
✗ Incorrect
Kadane's algorithm processes each element once, updating sums in constant time, so its time complexity is linear O(n).
🚀 Application
expertFind the maximum subarray sum and the subarray itself
Modify Kadane's algorithm to return both the maximum sum and the actual subarray that produces this sum for the input array [3, -2, 5, -1]. What is the maximum sum and the subarray?
DSA Go
func maxSubArray(nums []int) (int, []int) { maxSoFar := nums[0] maxEndingHere := 0 start := 0 end := 0 s := 0 for i, num := range nums { maxEndingHere += num if maxEndingHere > maxSoFar { maxSoFar = maxEndingHere start = s end = i } if maxEndingHere < 0 { maxEndingHere = 0 s = i + 1 } } return maxSoFar, nums[start : end+1] } func main() { nums := []int{3, -2, 5, -1} maxSum, subarray := maxSubArray(nums) println(maxSum) for _, v := range subarray { print(v, " ") } }
Attempts:
2 left
💡 Hint
Track start and end indices when updating max sum.
✗ Incorrect
The maximum sum is 6 from the subarray [3, -2, 5]. Adding -1 reduces the sum, so the best subarray ends before -1.
