Challenge - 5 Problems
Kadane's Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kadane's Algorithm on Mixed Array
What is the output of the following JavaScript code that uses Kadane's Algorithm to find the maximum subarray sum?
DSA Javascript
function maxSubArray(nums) {
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
Focus on how the algorithm updates the current sum and the maximum sum found so far.
✗ Incorrect
Kadane's algorithm finds the maximum sum of a contiguous subarray. For the input array, the maximum sum subarray is [4, -1, 2, 1] which sums to 6.
❓ Predict Output
intermediateMaximum Subarray Sum for All Negative Numbers
What will be the output of this Kadane's Algorithm implementation when all numbers are negative?
DSA Javascript
function maxSubArray(nums) {
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([-8, -3, -6, -2, -5, -4]));Attempts:
2 left
💡 Hint
Kadane's algorithm returns the largest single element if all are negative.
✗ Incorrect
When all numbers are negative, the maximum subarray is the single largest number, which is -2 here.
🧠 Conceptual
advancedUnderstanding Kadane's Algorithm Variables
In Kadane's Algorithm, what does the variable 'maxEndingHere' represent during the iteration?
Attempts:
2 left
💡 Hint
Think about how the algorithm decides to continue or start a new subarray at each step.
✗ Incorrect
'maxEndingHere' keeps track of the best sum of a subarray that ends exactly at the current position, helping decide whether to extend or start fresh.
❓ Predict Output
advancedOutput of Kadane's Algorithm with Single Element Array
What is the output of the following code when the input array has only one element?
DSA Javascript
function maxSubArray(nums) {
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([10]));Attempts:
2 left
💡 Hint
Consider how the algorithm initializes variables and the loop behavior with a single element.
✗ Incorrect
With one element, the maximum subarray sum is the element itself, 10.
❓ Predict Output
expertMaximum Subarray Sum with Zeroes and Positives
What is the output of this Kadane's Algorithm code for the input array containing zeroes and positive numbers?
DSA Javascript
function maxSubArray(nums) {
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([0, 0, 5, 0, 3, 0, 2]));Attempts:
2 left
💡 Hint
Sum the contiguous subarray with the largest total including zeroes.
✗ Incorrect
The maximum subarray is [5, 0, 3, 0, 2] which sums to 10.
