Quick Overview

Compute sum of longest positive subarray evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Compute sum of longest positive subarray

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given an integer array that may contain positive and negative numbers, find the sum of the longest contiguous subarray consisting only of positive numbers. If multiple such subarrays share the maximum length, return the maximum sum among them. If the array contains no positive numbers, return 0. Explain the algorithm and its time and space complexity, and provide code in a language of your choice.

Overview: Compute sum of longest positive subarray evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Given an integer array `nums` that may contain positive and negative numbers (and zeros), find the **sum of the longest contiguous subarray consisting only of positive numbers**. Rules: - A positive number is strictly greater than 0 (zero is NOT positive and breaks a run). - If multiple positive subarrays share the maximum length, return the **maximum sum** among them. - If the array contains no positive numbers, return `0`. **Example 1** ``` Input: [1, 2, -1, 3, 4, 5, -2, 6] Output: 12 ``` The positive runs are [1,2] (len 2), [3,4,5] (len 3), and [6] (len 1). The longest is [3,4,5], so the answer is 3+4+5 = 12. **Example 2 (tie on length)** ``` Input: [3, 4, -1, 10, 20] Output: 30 ``` Both [3,4] and [10,20] have length 2. Among the longest, the maximum sum is 10+20 = 30. **Example 3 (no positives)** ``` Input: [-1, -2, -3] Output: 0 ```

Constraints

  • 0 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • A 'positive' number is strictly > 0; zero breaks a run.
  • Return 0 when there are no positive numbers (including the empty array).
  • On a length tie, return the maximum sum among the longest runs.

Examples

Input: [1, 2, -1, 3, 4, 5, -2, 6]

Expected Output: 12

Explanation: Positive runs: [1,2] (len 2), [3,4,5] (len 3), [6] (len 1). Longest is [3,4,5] -> 3+4+5 = 12.

Input: [-1, -2, -3]

Expected Output: 0

Explanation: No positive numbers, so the answer is 0.

Hints

  1. Scan left to right. Whenever you see a positive number, extend the current run (length += 1, sum += value); on any non-positive number, reset the current run to empty.
  2. Track the best answer as a (length, sum) pair. Update it when the current run is strictly longer, OR when it ties the best length but has a larger sum — that handles the tie-breaking rule.
  3. Initialize best length and best sum to 0 so that an array with no positives naturally returns 0 without a special case.

Compute Sum of Longest Positive Subarray

mediumMicrosoftSoftware Engineer

Given an integer array nums that may contain positive and negative numbers (and zeros), find the sum of the longest contiguous subarray consisting only of positive numbers.

Rules:

  • A positive number is strictly greater than 0 (zero is NOT positive and breaks a run).
  • If multiple positive subarrays share the maximum length, return the maximum sum among them.
  • If the array contains no positive numbers, return 0.

Example 1

Input:  [1, 2, -1, 3, 4, 5, -2, 6]
Output: 12

The positive runs are [1,2] (len 2), [3,4,5] (len 3), and [6] (len 1). The longest is [3,4,5], so the answer is 3+4+5 = 12.

Example 2 (tie on length)

Input:  [3, 4, -1, 10, 20]
Output: 30

Both [3,4] and [10,20] have length 2. Among the longest, the maximum sum is 10+20 = 30.

Example 3 (no positives)

Input:  [-1, -2, -3]
Output: 0
View full question (all parts)

Compute sum of longest positive subarray

Given an integer array that may contain positive and negative numbers, find the sum of the longest contiguous subarray consisting only of positive numbers. If multiple such subarrays share the maximum length, return the maximum sum among them. If the array contains no positive numbers, return 0. Explain the algorithm and its time and space complexity, and provide code in a language of your choice.

Clarifying Questions to Ask

  • Clarify input sizes, value ranges, mutability, return format, and tie-breaking.
  • State the target time and space complexity before coding.
  • Call out edge cases such as empty inputs, duplicates, invalid values, overflow, and boundary sizes.

What a Strong Answer Covers

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?

Examples

Example 1
Input
[1, 2, -1, 3, 4, 5, -2, 6]
Output
12
Notes
Positive runs: [1,2] (len 2), [3,4,5] (len 3), [6] (len 1). Longest is [3,4,5] -> 3+4+5 = 12.
Example 2
Input
[-1, -2, -3]
Output
0
Notes
No positive numbers, so the answer is 0.

Constraints

  • •0 <= len(nums) <= 10^5
  • •-10^9 <= nums[i] <= 10^9
  • •A 'positive' number is strictly > 0; zero breaks a run.
  • •Return 0 when there are no positive numbers (including the empty array).
  • •On a length tie, return the maximum sum among the longest runs.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def longestPositiveSum(nums: list[int]) -> int:
Sample input
[1, 2, -1, 3, 4, 5, -2, 6]
Language
Case 1
Input
[1, 2, -1, 3, 4, 5, -2, 6]
Expected
12
Case 2
Input
[-1, -2, -3]
Expected
0

Browse More Questions