By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This single algorithm appears in 1 out of every 3 onsite interviews—master Kadane’s, and you instantly prove you can optimize dynamic problems in linear time. Miss it, and you risk failing a question that’s easier than it looks."
Before tackling Kadane’s Algorithm, ensure you understand: 1. Prefix Sums – How to compute cumulative sums of an array. 2. Dynamic Programming (DP) Basics – The concept of storing intermediate results to avoid recomputation. 3. Sliding Window (Optional but Helpful) – Useful for follow-up problems like "Maximum Subarray of Size K."
nums = [-2,1,-3,4,-1,2,1,-5,4]
[4,-1,2,1]
6
nums = [1,1,1], k=2
2
[1,1]
nums = [1,2,3,4], k=2
7
[3,4]
i
dp[i] = max(nums[i], dp[i-1] + nums[i])
Use this repeatable checklist for every Maximum Subarray problem:
0
Ask: "Is the array circular?" (If yes, handle wrap-around cases.)
Initialize Variables
max_current = nums[0]
max_global = nums[0] (Tracks overall max subarray found so far).
max_global = nums[0]
Iterate Through the Array
For each num in nums[1:]:
num
nums[1:]
max_current = max(num, max_current + num)
max_global = max(max_global, max_current)
Handle Edge Cases
max(nums)
If the array is empty, return 0 or raise an error (clarify with interviewer).
Follow-Up Questions (If Time Permits)
"What if we need the maximum product subarray?" → Track min/max products (handles negatives).
Optimize for Space (If Needed)
Kadane’s is already O(1) space. If asked to reduce further, note that it’s already optimal.
Test with Examples
[1,2,3]
[-1,-2,-3]
-1
[-2,1,-3,4,-1,2,1,-5,4]
[5]
5
Problem: Given an integer array nums, find the contiguous subarray with the largest sum.
nums
Thought Process: 1. Clarify: Subarray must be contiguous. Empty subarrays not allowed. 2. Initialize: max_current = max_global = nums[0]. 3. Iterate: - For i=1, nums[1] = 1 → max_current = max(1, -2 + 1) = 1 → max_global = max(-2, 1) = 1. - For i=2, nums[2] = -3 → max_current = max(-3, 1 + -3) = -2 → max_global = max(1, -2) = 1. - For i=3, nums[3] = 4 → max_current = max(4, -2 + 4) = 4 → max_global = max(1, 4) = 4. - Continue until end. 4. Result: max_global = 6 (subarray [4,-1,2,1]).
max_current = max_global = nums[0]
i=1
nums[1] = 1
max_current = max(1, -2 + 1) = 1
max_global = max(-2, 1) = 1
i=2
nums[2] = -3
max_current = max(-3, 1 + -3) = -2
max_global = max(1, -2) = 1
i=3
nums[3] = 4
max_current = max(4, -2 + 4) = 4
max_global = max(1, 4) = 4
max_global = 6
Code (Python):
def maxSubArray(nums): max_current = max_global = nums[0] for num in nums[1:]: max_current = max(num, max_current + num) max_global = max(max_global, max_current) return max_global
Time Complexity: O(n) (single pass). Space Complexity: O(1) (constant extra space).
Why This Works: - At each step, we decide whether to start a new subarray or extend the previous one. - max_current tracks the best subarray ending at the current index. - max_global ensures we never lose the best subarray found so far.
max_current
max_global
Problem: Return the start and end indices of the maximum subarray.
Thought Process: 1. Clarify: Need indices, not just the sum. 2. Initialize: - max_current = max_global = nums[0] - start = end = 0 - temp_start = 0 (tracks potential new subarray start) 3. Iterate: - If max_current + num < num, reset temp_start to current index. - Update max_current and max_global as before. - If max_global updates, set start = temp_start and end = current_index.
start = end = 0
temp_start = 0
max_current + num < num
temp_start
start = temp_start
end = current_index
def maxSubArrayWithIndices(nums): max_current = max_global = nums[0] start = end = 0 temp_start = 0 for i in range(1, len(nums)): if nums[i] > max_current + nums[i]: max_current = nums[i] temp_start = i else: max_current += nums[i] if max_current > max_global: max_global = max_current start = temp_start end = i return (start, end, max_global)
Example: nums = [-2,1,-3,4,-1,2,1,-5,4] → Returns (3, 6, 6) (subarray [4,-1,2,1]).
(3, 6, 6)
Why This Works: - We track temp_start to know when a new subarray begins. - When max_global updates, we record the new start and end.
start
end
Problem: Given a circular array, find the maximum subarray sum.
Thought Process: 1. Clarify: The array is circular (last element connects to first). 2. Key Insight: - The max subarray is either: - Normal (non-circular, solved with Kadane’s). - Circular (wraps around, e.g., [5,-3,5] → 5 + 5 = 10). 3. Approach: - Compute normal max (Kadane’s). - Compute total sum of array. - Compute min subarray (inverted Kadane’s). - Circular max = total_sum - min_subarray (if min_subarray != total_sum). - Return max(normal_max, circular_max).
[5,-3,5]
5 + 5 = 10
total_sum - min_subarray
min_subarray != total_sum
max(normal_max, circular_max)
def maxSubarrayCircular(nums): total = sum(nums) # Normal Kadane's (max subarray) max_normal = current_max = nums[0] for num in nums[1:]: current_max = max(num, current_max + num) max_normal = max(max_normal, current_max) # Inverted Kadane's (min subarray) min_subarray = current_min = nums[0] for num in nums[1:]: current_min = min(num, current_min + num) min_subarray = min(min_subarray, current_min) # Edge case: All numbers are negative if max_normal < 0: return max_normal # Circular max = total - min_subarray circular_max = total - min_subarray return max(max_normal, circular_max)
Example: nums = [5,-3,5] → - Normal max = 7 ([5,-3,5]). - Min subarray = -3. - Circular max = 10 - (-3) = 13 (but 10 is total sum, so 10 - (-3) = 13 is incorrect; actual circular max is 5 + 5 = 10). - Correction: Circular max = total - min_subarray = 7 - (-3) = 10. - Final answer = max(7, 10) = 10.
nums = [5,-3,5]
-3
10 - (-3) = 13
10
total - min_subarray = 7 - (-3) = 10
max(7, 10) = 10
Why This Works: - The circular max is the total sum minus the min subarray (since the min subarray is the "gap" we exclude). - If all numbers are negative, the normal max is the answer (no circular benefit).
max_global < 0
max_current = 0
max_current < 0
num > max_current + num
start/end
dp
max_global = 0
min_product
max_product
"Alright, let’s lock this in. Kadane’s Algorithm is your go-to for maximum subarray problems in linear time. Here’s the playbook:
Test with [1,2,3], [-1,-2,-3], and [-2,1,-3,4,-1,2,1,-5,4]. If you can code this in 5 minutes, you’re golden. Now go crush that interview!
You’ve got this! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.