Katkat

Compute sum of longest positive subarray

MicrosoftSoftware Engineer · Technical Screen

Last updated

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 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.