Maximize Products over Contiguous Nonempty Segments

Quick Overview

Find the maximum contiguous subarray product by retaining both positive and negative ending-product extremes.

Maximize Products over Contiguous Nonempty Segments

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the largest product of any nonempty contiguous subarray. A single-element subarray has that element's value. Elements cannot be skipped. ```cpp int maxProduct(vector<int>& nums); ``` ### Examples ```text Input: nums = [-2,3,-4] Output: 24 ``` The entire subarray has product 24. ```text Input: nums = [-3,0,-2] Output: 0 ``` The separated negative entries cannot be combined across the zero. ### Constraints - Length is between 1 and 20000. - Values lie between -10 and 10. - The product of every subarray, not only the maximum, fits a signed 32-bit integer.

Overview: Find the maximum contiguous subarray product by retaining both positive and negative ending-product extremes.

Google logo
Google
Oct 6, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

Problem

Return the largest product of any nonempty contiguous subarray. A single-element subarray has that element's value. Elements cannot be skipped.

int maxProduct(vector<int>& nums);

Examples

Input: nums = [-2,3,-4]
Output: 24

The entire subarray has product 24.

Input: nums = [-3,0,-2]
Output: 0

The separated negative entries cannot be combined across the zero.

Constraints

  • Length is between 1 and 20000.
  • Values lie between -10 and 10.
  • The product of every subarray, not only the maximum, fits a signed 32-bit integer.