PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

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.

  • Medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

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.

Quick Answer: 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.

Input: []

Expected Output: 0

Explanation: Empty array has no positive run; return 0.

Input: [5]

Expected Output: 5

Explanation: A single positive element forms the only (and longest) run, sum 5.

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

Expected Output: 15

Explanation: Both [1,2,3] and [4,5,6] have length 3; the larger sum is 4+5+6 = 15.

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

Expected Output: 30

Explanation: Tie on length (both len 2): [3,4]=7 vs [10,20]=30. Return the max sum, 30.

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

Expected Output: 3

Explanation: Zeros are not positive and break runs, so every run has length 1: [1],[2],[3]. Among the longest, the max sum is 3.

Input: [-5, 7, -8, 7, 7]

Expected Output: 14

Explanation: Runs are [7] (len 1, sum 7) and [7,7] (len 2, sum 14). The longer run wins regardless of sum comparison -> 14.

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.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)