Magicsheet logo

Minimum Positive Sum Subarray

Easy
12.5%
Updated 8/1/2025

Asked by 1 Company

Minimum Positive Sum Subarray

What is this problem about?

The Minimum Positive Sum Subarray problem asks you to find the minimum sum among all subarrays of a fixed length range that have a positive (greater than zero) sum. You're given an array of integers along with minimum and maximum subarray lengths, and you must identify the smallest positive sum achievable by any valid-length subarray. This Minimum Positive Sum Subarray coding problem blends sliding window mechanics with prefix sums.

Why is this asked in interviews?

Amazon includes this problem to test foundational array and prefix sum skills with a positivity constraint twist. It validates that candidates can efficiently enumerate subarray sums within a length range without recomputing sums from scratch. The array, sliding window, and prefix sum interview pattern directly applies here, making it a good signal for efficient thinking on array-range queries.

Algorithmic pattern used

Use prefix sums to compute any subarray sum in O(1). For each starting index i, iterate over valid ending indices within the length bounds. Compute sum as prefixSum[j+1] - prefixSum[i]. Track the minimum among all positive sums found. With n elements and length range [minLen, maxLen], this runs in O(n * (maxLen - minLen + 1)) time — acceptable for small length windows.

Example explanation

Array: [3, -2, 1, 4], minLen = 2, maxLen = 3.

  • Subarrays of length 2: [3,-2]=1, [-2,1]=-1, [1,4]=5.
  • Subarrays of length 3: [3,-2,1]=2, [-2,1,4]=3. Positive sums: 1, 5, 2, 3. Minimum positive sum = 1 from subarray [3,-2].

Common mistakes candidates make

  • Including subarrays with sum ≤ 0 in the minimum comparison.
  • Recomputing sums naively instead of using prefix sums.
  • Off-by-one errors in valid length bounds.
  • Returning infinity or crashing when no positive-sum subarray exists.

Interview preparation tip

Easy-level problems like this are often tests of implementation accuracy rather than creative insight. Build the habit of using prefix sum arrays for any problem involving "subarray sum in a range." After solving this, extend your practice to finding the minimum or maximum subarray sum within a length constraint — a common variant that appears at medium difficulty in technical screens.

Similar Questions