Maximum Subarray Sum
Updated
Problem statement
Given an integer array nums, return the largest possible sum of a non-empty contiguous subarray.
A subarray uses consecutive elements in their original order. You must select at least one element, even when all values are negative. Return the exact sum as a signed 64-bit integer.
Function
maxSubarraySum(nums: int[]) → longExamples
Example 1
nums = [4,-6,3,2,-1]return = 5The subarray [3,2] has the largest sum, 5. Extending it left or right decreases the sum.
Example 2
nums = [-4,-2,-7]return = -2The single-element subarray [-2] is optimal. Choosing no elements is not allowed.
Constraints
1 <= nums.length <= 20000-1000000 <= nums[i] <= 1000000
More Omnissa Onsite Interview questions
- Build and Check a Palindrome Linked ListONSITE INTERVIEW · Seen Sep 2026
- Find the CelebrityONSITE INTERVIEW · Seen Sep 2026
- Maximum All-Ones Square Side LengthONSITE INTERVIEW · Seen Sep 2026
- Coin ChangeONSITE INTERVIEW · Seen Jul 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Jul 2026
- Rotate Array RightONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- 0/1 Knapsack with Weight and Volume LimitsGoogle · ONSITE INTERVIEWGoogle interview questions
- Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Coin ChangeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Word BreakAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Maximum SubarrayUpstart · ONSITE INTERVIEWUpstart interview questions
- Smallest Sufficient TeamExpedia · ONSITE INTERVIEWExpedia interview questions
- Maximum Product SubarrayHartford Financial Services · ONSITE INTERVIEWHartford Financial Services interview questions
- Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions