Skip to main content

81. Maximum Product Subarray

mediumAsked at Vercel

Find the contiguous subarray within an integer array that has the largest product. Vercel asks this for the track-both-min-and-max trick — negatives flip the role, similar to how compounding latencies can flip favorable into unfavorable in their performance metrics.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel platform onsite; track-both-min-max expected.
  • Blind (2026-Q1)Listed in Vercel screen pool.

Problem

Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product. The test cases are generated so that the answer will fit in a 32-bit integer.

Constraints

  • 1 <= nums.length <= 2 * 10^4
  • -10 <= nums[i] <= 10
  • The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

Examples

Example 1

Input
nums = [2,3,-2,4]
Output
6

Explanation: [2,3] has product 6.

Example 2

Input
nums = [-2,0,-1]
Output
0

Approaches

1. Kadane-style single max

Track curMax = max(num, curMax * num). Update best.

Time
O(n)
Space
O(1)
// WRONG — a negative times a negative is positive, so a small negative product becomes a large positive when multiplied by the next negative. Need to track BOTH.

Tradeoff: Fails on inputs like [-2, 3, -4] (correct = 24).

2. Track curMin AND curMax (optimal)

Maintain curMax and curMin. At each step, the new candidates are num, num*curMax, num*curMin. The new curMax/curMin are max/min of those three.

Time
O(n)
Space
O(1)
function maxProduct(nums) {
  let curMax = nums[0], curMin = nums[0], best = nums[0];
  for (let i = 1; i < nums.length; i++) {
    const n = nums[i];
    const candidates = [n, n * curMax, n * curMin];
    curMax = Math.max(...candidates);
    curMin = Math.min(...candidates);
    if (curMax > best) best = curMax;
  }
  return best;
}

Tradeoff: Single pass, O(1) space. The dual tracking handles the sign-flip from negatives: today's small negative can become tomorrow's large positive.

Vercel-specific tips

Vercel grades the dual-tracking insight. Bonus signal: explaining WHY a single Kadane fails (negative * negative = positive flips the role of min and max) and articulating that the 'three candidates' must be considered at each step.

Common mistakes

  • Single max tracking — fails on negative-negative pairs.
  • Not including `n` as a standalone candidate — fails when a zero resets the chain.
  • Updating curMax before reading it for curMin — corrupts the calculation.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Maximum Product of Three Numbers (LC 628) — different approach.
  • Maximum Sum Subarray (LC 53) — single-min-max suffices.
  • Product of Array Except Self (LC 238) — prefix/suffix products.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why both min AND max?

Because multiplying by a negative swaps min and max. Today's curMin might be -10; tomorrow's num is -3; -10 * -3 = 30 = tomorrow's curMax.

Why include `n` as a standalone candidate?

To allow restarting the subarray. If curMax = -50 and n = 5, the best subarray ending at i is just [5], not [...prev, 5].

Practice these live with InterviewChamp.AI

Drill Maximum Product Subarray and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →