Coding interview problems › Arrays & Hashing
#️⃣ Arrays & Hashing Medium
Maximum Subarray (Kadane's Algorithm)
Kadane's algorithm explained with an example: find the maximum-sum subarray in one O(n) pass, all-negative edge case, tested Python and a live visual.
Also searched as: Kadane's algorithm example · largest sum contiguous subarray · maximum subarray sum
Try it live Free
Prefix Sums & Kadane
Watch the O(n²) brute force compared with Kadane's single pass, and see the running sum dropped the moment it goes negative. Try an all-negative array to check the edge case.
The problem
Given a non-empty list of integers (positive, negative or zero), find the largest possible sum of a contiguous, non-empty subarray.
The subarray [4, -1, 2, 1].
All negative: the best is the single largest element.
The key insight
Walk left to right and keep the best sum of a subarray that ends here. At each element you have one choice: extend the previous run or start fresh. A run whose sum has gone negative can only hurt whatever comes next, so you drop it. That single decision per element is Kadane's algorithm.
Step-by-step approach
- Set
cur = best = nums[0]. - For each next element
x:cur = max(x, cur + x)(start fresh or extend). - Update
best = max(best, cur). - Return
best. Initialising withnums[0]instead of 0 keeps the all-negative case correct.
Python solution
def max_subarray(nums):
cur = best = nums[0]
for x in nums[1:]:
cur = max(x, cur + x) # extend the run, or restart at x
best = max(best, cur)
return bestComplexity
One pass with two running numbers. No extra arrays are needed.
Common mistakes
- Initialising
best = 0, which returns 0 for an all-negative list instead of the largest element. - Resetting
curto 0 when it goes negative but updatingbestin the wrong order, so a single negative answer is lost. - Confusing subarray (contiguous) with subsequence (any elements), which is a different and easier problem.
Frequently asked questions
What is the time and space complexity of Kadane's algorithm?
O(n) time and O(1) space: one pass that tracks the best sum ending at the current element and the best sum seen overall.
How do I return the subarray itself, not just the sum?
Track a start index that resets whenever you restart at x, and record start and end whenever best improves.
Why is Kadane's algorithm considered dynamic programming?
cur is the answer to a subproblem, the best sum of a subarray ending at index i, built from the answer at i - 1. Keeping only the previous value is the space-optimised form of that DP.
What about the maximum circular subarray?
Take the larger of the normal Kadane answer and total sum minus the minimum subarray sum, unless every element is negative, in which case use the normal answer.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Arrays & Hashing Hash Tables lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.