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 Typescript
function maxSubArray(nums: number[]): number {
let maxSoFar = nums[0];
let maxEndingHere = nums[0];
for (let i = 1; i < nums.length; i++) {
maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
maxSoFar = Math.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
console.log(maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4]));Attempts:
2 left
💡 Hint
Think about the continuous subarray with the largest sum.
✗ Incorrect
Kadane's algorithm finds the maximum sum of a continuous subarray. Here, the subarray [4, -1, 2, 1] sums to 6, but the subarray [4, -1, 2, 1, -5, 4] sums to 7, which is the maximum.
🧠 Conceptual
intermediateWhat does Kadane's algorithm track during iteration?
Kadane's algorithm uses two variables during iteration over the array. What do these variables represent?
Attempts:
2 left
💡 Hint
One variable resets when sum becomes negative, the other keeps track of the best result.
✗ Incorrect
Kadane's algorithm keeps track of the maximum sum of subarrays ending at the current position and the maximum sum found overall.
🔧 Debug
advancedIdentify the error in this Kadane's algorithm implementation
What error does this code produce when run on array [-1, -2, -3]?
function maxSubArray(nums: number[]): number {
let maxSoFar = 0;
let maxEndingHere = 0;
for (let i = 0; i < nums.length; i++) {
maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
maxSoFar = Math.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
console.log(maxSubArray([-1, -2, -3]));
Attempts:
2 left
💡 Hint
Check initial values of maxSoFar and maxEndingHere.
✗ Incorrect
Initializing maxSoFar and maxEndingHere to 0 causes incorrect result when all numbers are negative or when max subarray sum is less than 0. It should start with nums[0].
❓ Predict Output
advancedWhat is the maximum subarray sum for all negative numbers?
What does Kadane's algorithm return for the array [-4, -2, -7, -3]?
DSA Typescript
function maxSubArray(nums: number[]): number {
let maxSoFar = nums[0];
let maxEndingHere = nums[0];
for (let i = 1; i < nums.length; i++) {
maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
maxSoFar = Math.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
console.log(maxSubArray([-4, -2, -7, -3]));Attempts:
2 left
💡 Hint
Kadane's algorithm returns the largest single element if all are negative.
✗ Incorrect
The maximum subarray sum is the largest element -2 because any longer subarray sum is smaller.
🚀 Application
expertHow to modify Kadane's algorithm to return the subarray itself?
Which modification allows Kadane's algorithm to return the actual maximum sum subarray, not just the sum?
Attempts:
2 left
💡 Hint
Keep track of where the current subarray starts and update when you find a better sum.
✗ Incorrect
By tracking the start index when maxEndingHere resets and updating the end index when maxSoFar updates, you can return the subarray with maximum sum.
