Kadane's Algorithm finds the maximum sum of a contiguous subarray within a one-dimensional array of numbers.
Click to reveal answer
beginner
What is the main idea behind Kadane's Algorithm?
It keeps track of the current subarray sum and resets it if it becomes negative, while updating the maximum sum found so far.
Click to reveal answer
intermediate
In Kadane's Algorithm, what does the variable 'current_sum' represent?
'current_sum' represents the sum of the current subarray being considered. It resets when the sum becomes negative to start fresh from the current element.
Click to reveal answer
intermediate
Why does Kadane's Algorithm reset the current sum when it becomes negative?
Because a negative sum will only reduce the sum of any future subarray, so it's better to start a new subarray from the current element.
Click to reveal answer
beginner
What is the time complexity of Kadane's Algorithm and why?
The time complexity is O(n) because it scans the array only once, updating sums and maximums in constant time per element.
Click to reveal answer
What does Kadane's Algorithm return?
AMaximum element in the array
BMaximum sum of a contiguous subarray
CMinimum sum of a contiguous subarray
DSum of all elements in the array
✗ Incorrect
Kadane's Algorithm finds the maximum sum of any contiguous subarray.
When does Kadane's Algorithm reset the current sum?
AWhen current sum is negative
BWhen current sum is positive
CWhen current sum is zero
DNever resets
✗ Incorrect
It resets current sum when it becomes negative to avoid reducing future sums.
What is the initial value of the maximum sum in Kadane's Algorithm?
ANegative infinity or very small number
BSum of all elements
CFirst element of the array
D0
✗ Incorrect
Maximum sum starts very small to ensure any subarray sum will update it.
What is the time complexity of Kadane's Algorithm?
AO(log n)
BO(n log n)
CO(n^2)
DO(n)
✗ Incorrect
Kadane's Algorithm runs in linear time by scanning the array once.
Which of these is NOT true about Kadane's Algorithm?
AIt finds the maximum sum of contiguous subarray
BIt can handle negative numbers
CIt works only for arrays with positive numbers
DIt uses a running sum and maximum sum variables
✗ Incorrect
Kadane's Algorithm works for arrays with negative numbers as well.
Explain how Kadane's Algorithm finds the maximum sum of a contiguous subarray.
Think about how the algorithm decides to keep or discard parts of the subarray.
You got /4 concepts.
Describe why Kadane's Algorithm is efficient compared to checking all subarrays.
Compare it to a brute force approach that checks every subarray.
You got /4 concepts.
Practice
(1/5)
1. What does Kadane's Algorithm primarily help to find in an array?
easy
A. The longest increasing subsequence
B. The minimum element in the array
C. The total sum of all elements
D. The maximum sum of a contiguous subarray
Solution
Step 1: Understand the problem Kadane's Algorithm solves
Kadane's Algorithm is designed to find the maximum sum of any contiguous subarray within a given array.
Step 2: Compare with other options
Options B, C, and D describe different problems unrelated to Kadane's Algorithm.
Final Answer:
The maximum sum of a contiguous subarray -> Option D
Quick Check:
Maximum subarray sum = maximum sum of contiguous elements [OK]
Hint: Kadane finds max sum in continuous parts of array [OK]
Common Mistakes:
Confusing maximum subarray sum with total array sum
Thinking it finds minimum or longest subsequence
Assuming it sorts the array
2. Which of the following is the correct initialization for Kadane's Algorithm variables in Python?
easy
A. max_so_far = 0; max_ending_here = float('inf')
B. max_so_far = 0; max_ending_here = 0
C. max_so_far = float('-inf'); max_ending_here = 0
D. max_so_far = null; max_ending_here = null
Solution
Step 1: Understand variable roles in Kadane's Algorithm
max_so_far tracks the maximum sum found so far, so it should start very low (negative infinity) to handle negative numbers. max_ending_here tracks current subarray sum and starts at 0.
Step 2: Check each option
max_so_far = 0; max_ending_here = 0 initializes max_so_far to 0, which fails if all numbers are negative. max_so_far = 0; max_ending_here = float('inf') wrongly sets max_ending_here to positive infinity. max_so_far = null; max_ending_here = null uses null, which causes errors in arithmetic.
Final Answer:
max_so_far = float('-inf'); max_ending_here = 0 -> Option C
Quick Check:
Initialize max_so_far very low, max_ending_here zero [OK]
Hint: Start max_so_far very low to handle negatives [OK]
Common Mistakes:
Initializing max_so_far to 0 causes wrong results with negatives
Using null causes type errors
Setting max_ending_here to infinity breaks logic
3. What is the output of the following code?
def max_subarray_sum(arr):
max_so_far = float('-inf')
max_ending_here = 0
for x in arr:
max_ending_here += x
if max_so_far < max_ending_here:
max_so_far = max_ending_here
if max_ending_here < 0:
max_ending_here = 0
return max_so_far
print(max_subarray_sum([-2, 3, 2, -1, 4, -5]))
medium
A. 8
B. 7
C. 6
D. 5
Solution
Step 1: Trace the loop and update sums
Start max_so_far = float('-inf'), max_ending_here = 0 Iteration 1: x=-2, max_ending_here= -2; max_so_far updated to -2; -2<0 reset to 0 (max_so_far=-2) Iteration 2: x=3, max_ending_here=3 > -2, max_so_far=3 Iteration 3: x=2, max_ending_here=5 >3, max_so_far=5 Iteration 4: x=-1, max_ending_here=4 <5 no update, 4>=0 no reset Iteration 5: x=4, max_ending_here=8 >5, max_so_far=8 Iteration 6: x=-5, max_ending_here=3 <8 no update, 3>=0 no reset
Step 2: Final return value
max_so_far is 8, so the function returns 8.
Final Answer:
8 -> Option A
Quick Check:
Max subarray sum = 8 [OK]
Hint: Add elements, reset sum if negative, track max sum [OK]
Common Mistakes:
Not resetting max_ending_here when negative
Updating max_so_far incorrectly
Miscounting sums during iteration
4. Identify the bug in this Kadane's Algorithm implementation:
def max_subarray(arr):
max_so_far = 0
max_ending_here = 0
for i in range(len(arr)):
max_ending_here = max_ending_here + arr[i]
if max_so_far < max_ending_here:
max_so_far = max_ending_here
elif max_ending_here < 0:
max_ending_here = 0
return max_so_far
print(max_subarray([-3, -2, -1]))
medium
A. max_so_far initialized to 0 causes wrong result for all negatives
B. Using elif instead of if causes skipping reset of max_ending_here
C. Loop should start from 1, not 0
D. max_ending_here should be initialized to float('-inf')
Solution
Step 1: Trace execution on [-3, -2, -1]
max_so_far=0, max_ending_here=0. i=0, x=-3: meh=-3; 0 < -3? no; elif -3<0: yes, meh=0. i=1, x=-2: meh=-2; no update; reset meh=0. i=2, x=-1: meh=-1; no update; reset. Returns 0, but correct max subarray sum is -1.
Step 2: Why A and not others
Init max_so_far=0 prevents update when all negative (meh never >0). B: elif ok here (if never true, always resets). C: range correct. D: meh=0 correct.
Final Answer:
max_so_far initialized to 0 causes wrong result for all negatives -> Option A
Quick Check:
Init max_so_far to float('-inf') for negatives [OK]
Hint: Initialize max_so_far to float('-inf') to handle all-negative arrays [OK]
Common Mistakes:
Initializing max_so_far to 0 fails with all negatives
Using elif causes reset to be skipped
Wrong loop range causes index errors
5. Given an array of integers, how can Kadane's Algorithm be adapted to also return the start and end indices of the maximum sum subarray? Choose the best approach.
hard
A. Use a nested loop to check all subarrays and record indices
B. Track current start index when resetting max_ending_here; update global start and end when max_so_far changes
C. Store all subarrays and pick the max sum after the loop ends
D. Return max_so_far only; indices cannot be found with Kadane's Algorithm
Solution
Step 1: Understand Kadane's Algorithm extension
To find indices, track the start index of the current subarray when max_ending_here resets to 0. When max_so_far updates, record current start and end indices.
Step 2: Compare with other options
Store all subarrays and pick the max sum after the loop ends is inefficient and not Kadane's approach. Use a nested loop to check all subarrays and record indices is brute force, not Kadane's. Return max_so_far only; indices cannot be found with Kadane's Algorithm is incorrect; indices can be found.
Final Answer:
Track current start index when resetting max_ending_here; update global start and end when max_so_far changes -> Option B
Quick Check:
Track start on reset, update indices on max update [OK]
Hint: Track start index on reset, update when max_so_far changes [OK]
Common Mistakes:
Ignoring index tracking in Kadane's Algorithm
Using brute force instead of Kadane's logic
Assuming indices can't be found with Kadane's
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.