Maximum Product Subarray
Reported by candidates from Hartford Financial Services's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Hartford Financial Services reported this one in September 2026, and it looks easy until a negative number shows up. Maximum Product Subarray is a dynamic programming problem in disguise, and the naive approach of tracking only a running max fails on inputs like [-2,3,-4]. If you've got the OA coming in a day or two, this is a pattern worth locking in now. Know the trick, know the zero case, and you're done in ten minutes. If you blank mid-assessment, StealthCoder sits invisibly on your screen as a safety net and hands you the solution while the proctor sees nothing.
The problem
Given a nonempty integer array nums, return the largest product obtainable by multiplying every element in one nonempty contiguous subarray. A contiguous subarray uses consecutive elements of nums. You may choose a single element, and the array may contain negative numbers and zeros. Return the product value, rather than the chosen subarray or its indices. Products are exact signed integers; do not apply a modulus. Function maxProduct(nums: int[]) → long Examples Example 1 nums = [2,3,-2,4] return = 6 The subarray [2,3] has product 6. Any subarray containing -2 has a negative product, and the remaining singleton [4] has product 4. Example 2 nums = [-2,3,-4] return = 24 The entire array is contiguous, and its product is (-2) * 3 * (-4) = 24. The two negative factors produce a positive product. Example 3 nums = [-2,0,-1] return = 0 The singleton [0] has product 0, larger than either negative singleton. The values -2 and -1 cannot be multiplied together without including the intervening zero. Constraints 1 <= nums.length <= 10^5. -10 <= nums[i] <= 10. The product of every nonempty contiguous subarray is guaranteed to lie between -10^18 and 10^18, inclusive. The returned value must use a signed 64-bit integer or a wider exact integer type.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: track both the maximum and the minimum product ending at the current index. A negative number flips them, so the most negative product can become the largest after one more negative multiplication. At each element x, compute candidates x, curMax*x, curMin*x. New max is the biggest, new min is the smallest. Update the global answer with the max. Zeros reset things automatically because x itself is a candidate, which is why [-2,0,-1] returns 0. The common pitfall is tracking only the max, which gives 6 instead of 24 on Example 2. Another is overwriting curMax before computing curMin, so use a temp variable. It's O(n) time and O(1) space. Use a 64-bit integer since products reach 10^18. If the swap logic slips under pressure, StealthCoder is the hedge for the live OA, giving you the working code when your head goes blank.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Maximum Product Subarray cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as maximum product subarray. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Hartford Financial Services's OA.
Hartford Financial Services reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Product Subarray FAQ
What's the trick to Maximum Product Subarray?+
Keep both the max and min product ending at each index. A negative number swaps their roles, so yesterday's minimum can become today's maximum. Update the answer with the max each step. It's a single pass with two variables.
How hard is this really for the Hartford Financial Services OA?+
Medium difficulty. The code is about ten lines, but the idea of tracking a minimum is what trips people up. If you've seen it once, it's easy. If you haven't, you'll probably write the max-only version and fail on negatives.
How do zeros affect the solution?+
A zero collapses both running max and min to zero, which is correct because no subarray can span it profitably. Since each step considers x alone as a candidate, the product restarts cleanly after the zero. No special case code is needed.
Do I need a long type for this problem?+
Yes. The constraints guarantee products stay within 10^18, which overflows a 32-bit int but fits in a signed 64-bit integer. Use long in Java or C++, and Python handles it natively. No modulus applies here.
How do I prepare in 48 hours?+
Write the two-variable solution from memory twice. Then test it by hand on [2,3,-2,4], [-2,3,-4], and [-2,0,-1]. Those three cases cover positives, double negatives, and zeros. Also skim related prefix and suffix product ideas as a backup approach.