Maximum Product Subarray
Updated
Problem statement
You are given an integer array nums. Return the largest product you can get from a block of neighboring elements.
A block is a run of one or more elements that sit next to each other, in their original order. A block must have at least one element, so the empty block is not allowed.
Function
maxProduct(nums: int[]) → intExamples
Example 1
nums = [-2,3,-4]return = 24The whole array has product (-2) x 3 x (-4) = 24. Two negative numbers multiply to a positive one, so the whole array is the best block.
Smaller blocks do worse: [-2, 3] is -6, [3, -4] is -12, and the single values are -2, 3 and -4.
Example 2
nums = [0,2,-3,0,6]return = 6A zero turns every block that contains it into 0. So only the pieces between zeros matter: [2, -3] and [6].
The best blocks there are [2] with product 2 and [6] with product 6. The answer is 6.
Example 3
nums = [-5]return = -5The block must have at least one value, so the only block is [-5]. Its product is -5.
Example 4
nums = [2,-5,-2,-4,3]return = 24The block [-2, -4, 3] has product (-2) x (-4) x 3 = 24.
Other blocks are smaller: [2, -5, -2] is 20, [-5, -2] is 10, [-2, -4] is 8, and [2, -5, -2, -4, 3] is -240. No block beats 24.
Constraints
1 <= nums.length <= 20000-10 <= nums[i] <= 10- The product of every block of neighboring elements fits in a signed 32-bit integer.
More Bloomberg LP Onsite Interview questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Currency Conversion RatePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Maximal SquareAmazon · ONSITE INTERVIEW
- Distinct Subset SumsGoogle · ONSITE INTERVIEWGoogle interview questions
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Coin ChangeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Maximum SubarrayUpstart · ONSITE INTERVIEWUpstart interview questions
- Smallest Sufficient TeamExpedia · ONSITE INTERVIEWExpedia interview questions
- Maximum Product SubarrayHartford Financial Services · ONSITE INTERVIEWHartford Financial Services interview questions
- Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions
Interview reports that mention this question
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)