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 Rust
fn max_subarray_sum(nums: &[i32]) -> i32 {
let mut max_ending_here = 0;
let mut max_so_far = i32::MIN;
for &x in nums {
max_ending_here = (max_ending_here + x).max(x);
max_so_far = max_so_far.max(max_ending_here);
}
max_so_far
}
fn main() {
let arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
println!("{}", max_subarray_sum(&arr));
}Attempts:
2 left
💡 Hint
Look for the contiguous subarray with the largest sum.
✗ Incorrect
The maximum subarray is [4, -1, 2, 1] which sums to 6.
❓ Predict Output
intermediateWhat is the maximum subarray sum for all negative numbers?
Given the array [-8, -3, -6, -2, -5, -4], what does Kadane's algorithm return as the maximum subarray sum?
DSA Rust
fn max_subarray_sum(nums: &[i32]) -> i32 {
let mut max_ending_here = 0;
let mut max_so_far = i32::MIN;
for &x in nums {
max_ending_here = (max_ending_here + x).max(x);
max_so_far = max_so_far.max(max_ending_here);
}
max_so_far
}
fn main() {
let arr = [-8, -3, -6, -2, -5, -4];
println!("{}", max_subarray_sum(&arr));
}Attempts:
2 left
💡 Hint
Kadane's algorithm returns the largest single element if all are negative.
✗ Incorrect
The largest number in the array is -2, so the maximum subarray sum is -2.
🔧 Debug
advancedWhat error occurs when running this Kadane's algorithm variant?
Consider this Rust code variant of Kadane's algorithm. What error will it produce when compiled or run?
DSA Rust
fn max_subarray_sum(nums: &[i32]) -> i32 {
let mut max_ending_here = 0;
let mut max_so_far = 0;
for &x in nums {
max_ending_here = (max_ending_here + x).max(x);
max_so_far = max_so_far.max(max_ending_here);
}
max_so_far
}
fn main() {
let arr = [-8, -3, -6, -2, -5, -4];
println!("{}", max_subarray_sum(&arr));
}Attempts:
2 left
💡 Hint
Check initial value of max_so_far for correctness.
✗ Incorrect
Initializing max_so_far to 0 causes it to ignore negative sums, leading to an incorrect maximum sum of 0 instead of -2.
🧠 Conceptual
advancedWhat is the time complexity of Kadane's algorithm?
Kadane's algorithm finds the maximum subarray sum in an array. What is its time complexity?
Attempts:
2 left
💡 Hint
The algorithm scans the array once.
✗ Incorrect
Kadane's algorithm processes each element once, so it runs in linear time O(n).
🚀 Application
expertHow to modify Kadane's algorithm to return the subarray itself?
Which code snippet correctly modifies Kadane's algorithm to return the start and end indices of the maximum subarray along with the sum?
Attempts:
2 left
💡 Hint
Remember to track where the current subarray starts and update indices when max sum improves.
✗ Incorrect
To find the subarray, track the start index when max_ending_here resets and update start/end when max_so_far updates.
