Recall & Review
beginner Click to reveal answer
What problem does Kadane's Algorithm solve?
Kadane's Algorithm finds the maximum sum of a contiguous subarray within a one-dimensional array of numbers.
beginner Click to reveal answer
What is the main idea behind Kadane's Algorithm?
It keeps track of the current subarray sum and updates it as max(current_sum + num, num), while updating the maximum sum found so far.
intermediate Click to reveal answer
In Kadane's Algorithm, what does resetting the current sum represent?
It means starting a new subarray from the next element because continuing the current subarray would reduce the sum.
beginner Click to reveal answer
What is the time complexity of Kadane's Algorithm?
O(n), where n is the number of elements in the array, because it processes each element once.
intermediate Click to reveal answer
Show the Rust code snippet for Kadane's Algorithm to find the maximum subarray sum.
fn max_subarray_sum(arr: &[i32]) -> i32 {
let mut max_sum = arr[0];
let mut current_sum = arr[0];
for &num in &arr[1..] {
current_sum = std::cmp::max(current_sum + num, num);
max_sum = std::cmp::max(max_sum, current_sum);
}
max_sum
}What does Kadane's Algorithm return for the array [-2, 1, -3, 4, -1, 2, 1, -5, 4]?
✗ Incorrect
The maximum subarray is [4, -1, 2, 1] which sums to 6.
What happens when the current sum becomes negative in Kadane's Algorithm?
✗ Incorrect
The algorithm resets the current sum to the current element if that is greater than continuing the sum.
What is the initial value of max_sum in Kadane's Algorithm?
✗ Incorrect
max_sum starts as the first element to handle arrays with all negative numbers.
Which of these is NOT a step in Kadane's Algorithm?
✗ Incorrect
Kadane's Algorithm does not require sorting the array.
What is the space complexity of Kadane's Algorithm?
✗ Incorrect
It uses only a few variables, so space complexity is constant O(1).
Explain how Kadane's Algorithm finds the maximum sum of a contiguous subarray.
Think about how the algorithm decides to start a new subarray.
You got /4 concepts.
Write the Rust function signature and describe the key variables used in Kadane's Algorithm.
Focus on input type and purpose of variables.
You got /4 concepts.
