Coding · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Maximum Subarray
Problem
Given an integer array nums, find the contiguous, non-empty subarray with the largest sum and return that sum.
- The array is non-empty and has at most 200,000 elements.
- Elements are integers with magnitude at most 50,000, so sums fit in 64 bits.
Examples
nums = [3, -4, 5, 2, -1, 6, -8, 2]→12. The subarray[5, 2, -1, 6].nums = [2, 9, -3, 4, 6]→18. The whole array.nums = [-8, -2, -5]→-2. All negative, so the best subarray is the single element-2.
Hints
- For each position, consider the best subarray that ends exactly there. How does it relate to the best subarray ending one position earlier?
- Extending a previous subarray is only worth it if that subarray's sum is positive; otherwise start fresh.
- This is Kadane's algorithm:
best_ending_here = max(x, best_ending_here + x), and the answer is the max of those over all positions.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.