Maximum Subarray
Updated
Problem statement
Given a nonempty integer array nums, return the largest sum of any nonempty contiguous subarray. A subarray uses consecutive elements in their original order.
For this exercise, assume the result is an exact signed 64-bit integer. You must choose at least one element, including when all values are negative.
Function
maxSubarraySum(nums: int[]) → longExamples
Example 1
nums = [-2,1,-3,4,-1,2,1,-5,4]return = 6Interior maximum across several positive runs.
Example 2
nums = [-8,-3,-6]return = -3All negative must not return empty zero.
Example 3
nums = [5]return = 5Single positive.
Constraints
1 <= nums.length <= 1000.-10^9 <= nums[i] <= 10^9.- Use signed 64-bit arithmetic for running sums and the result.
More Upstart interview questions
- Babysitter Quote with Hourly RatesONSITE INTERVIEW · Seen Oct 2026
- Buffet Day GainsOA · Seen Sep 2026
- Case-Insensitive Anagram CheckOA · Seen Sep 2026
- Filter Duplicates While Preserving OrderOA · Seen Sep 2026
- Scale Ingredient QuantitiesOA · Seen Sep 2026
- ABO Parent Genotype CombinationsOA · Seen Aug 2026
- Chunked Byte Stream ChecksumsOA · Seen Aug 2026
- Cluster Size on DiskOA · Seen Aug 2026
- Double Substitution DecryptionOA · Seen Aug 2026
- Add Drama to Text GroupsOA · Seen Jul 2026
Similar questions at other companies
- Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
- Distinct Subset SumsGoogle · ONSITE INTERVIEWGoogle interview questions
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- 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
- Smallest Sufficient TeamExpedia · ONSITE INTERVIEWExpedia interview questions
- Maximum Product SubarrayHartford Financial Services · ONSITE INTERVIEWHartford Financial Services interview questions
- Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions
- Maximum Value from Circular HousesDigitalOcean · ONSITE INTERVIEWDigitalOcean interview questions