FastPrepMaximum Product Subarray

Maximum Product Subarray

Google logoGoogle● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

Given an integer array nums, return the largest product of any non-empty contiguous subarray.

Function

maxProduct(nums: int[]) → int

Examples

Example 1

nums = [2,3,-2,4]return = 6

The contiguous subarray [2,3] has product 6.

Example 2

nums = [-2,0,-1]return = 0

The two negative values are separated by 0, so they cannot belong to the same contiguous subarray.

Example 3

nums = [-2,3,-4]return = 24

The entire array has product 24.

Constraints

  • 1 <= nums.length <= 20000.
  • -10 <= nums[i] <= 10.
  • The product of every contiguous subarray fits in a signed 32-bit integer.

More Google problems

See Google hiring insights
public int maxProduct(int[] nums) {
    // Write your code here.
}
nums[2,3,-2,4]
expected6
Checking account…