Maximum Subarray
Updated
Problem statement
Given an integer array nums, return the largest possible sum of a nonempty contiguous subarray.
Function
maxSubArray(nums: int[]) → longExamples
Example 1
nums = [-2,1,-3,4,-1,2,1,-5,4]return = 6The subarray [4,-1,2,1] has the maximum sum 6.
Example 2
nums = [1]return = 1The only nonempty subarray is the single element.
Example 3
nums = [-8,-3,-6,-2,-5,-4]return = -2When every value is negative, the best nonempty subarray is the largest individual value.
Constraints
1 <= nums.length <= 100000.-1000000000 <= nums[i] <= 1000000000.- The answer fits in a signed 64-bit integer.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Best Time to Buy and Sell StockAmazon · ONSITE INTERVIEW
- Best Time to Buy and Sell StockKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Best Time to Buy and Sell StockOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Maximum SubarraySquarepoint Capital · ONSITE INTERVIEWSquarepoint Capital interview questions
- Best Time to Buy and Sell StockZoox · ONSITE INTERVIEW
- Maximum Subarray SumSafe Security · ONSITE INTERVIEW
- Pascal's TriangleEricsson · ONSITE INTERVIEWEricsson interview questions
- Best Time to Buy and Sell StockMotive · ONSITE INTERVIEWMotive interview questions
- Best Time to Buy and Sell StockBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Maximum Profit with Repeated Stock TradesPalantir · ONSITE INTERVIEWPalantir interview questions