Maximum Product Subarray
Problem
Given an integer array nums, find a contiguous, non-empty subarray whose elements multiply to the largest possible value, and return that product. The product of any prefix or subarray is guaranteed to fit in a 32-bit signed integer.
numsholds up to about twenty thousand integers.- Each value is small (say, between -10 and 10), and every intermediate product fits comfortably in a normal signed integer.
Examples
nums = [3, 4, -3, 5]→12, from the subarray[3, 4]. Including the-3flips the sign.nums = [-5, 0, -2]→0. Any subarray containing both negatives also contains the zero.nums = [-3, 5, -2]→30. Two negatives multiply to a positive, so the whole array is best.
Hints
- Try the maximum-sum-subarray (Kadane) idea, keeping the best product ending at each index. Where does it fail?
- A very negative running product is one negative number away from becoming very positive. What second quantity should you track?
- Keep both the maximum and minimum product ending at the current index; a negative element swaps their roles.
Last reviewed 8 September 2026
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.
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.