Maximum Product Subarray Problem Statement

Given an array of integers, determine the contiguous subarray that produces the maximum product of its elements.

Explanation:

A subarray can be derived from the original array by deleting certain elements from the beginning and/or the end. You need to identify which subarray gives the highest product.

Input:

The first line contains an integer 'T', denoting the number of test cases.
For each test case, the first line contains an integer N, representing the number of elements in the array 'arr'.
The second line contains N space-separated integers, which are the elements of 'arr'.

Output:

Output the maximum product of a contiguous subarray for each test case. The output for each test case should be printed on a new line.

Example:

Input:
If arr = {-3, 4, 5}
Subarray Products:
The possible non-empty contiguous subarrays: {-3}, {4}, {5}, {-3, 4}, {4, 5}, {-3, 4, 5}
Their products: -3, 4, 5, -12, 20, -60
Output:
Maximum product is 20
Constraints:
  • 1 <= T <= 100
  • 1 <= N <= 5000
  • -100 <= arr[i] <= 100
Note:

You do not need to print anything explicitly. The function implementation should return the result.

Follow-Up:

Consider solving the problem in linear time with constant space complexity.

AnswerBot
4mo

Find the contiguous subarray with the maximum product of elements in an array.

  • Iterate through the array and keep track of the maximum and minimum product ending at each index.

  • Update the maximum produc...read more

Help your peers!
Select
Add answer anonymously...

Directi Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Merge Overlapping Intervals Problem Statement Given a specified number of interv...read more
A Software Developer Intern was asked Q. Design a streaming service similar to Netflix and explain how it onboard new con...read more
A Software Developer Intern was asked Q. Postfix Expression Evaluation Problem Statement Given a postfix expression, your...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Merge Overlapping Intervals Problem Statement Given a specified number of interv...read more
A Software Developer Intern was asked Q2. Design a streaming service similar to Netflix and explain how it onboard new con...read more
A Software Developer Intern was asked Q3. Postfix Expression Evaluation Problem Statement Given a postfix expression, your...read more
Directi Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits