Maximum Subarray Sum
Updated
Problem statement
You are given a nonempty integer array values. Return the largest sum of any one nonempty contiguous subarray. A contiguous subarray is a run of elements that sit next to each other.
A subarray holds every element from an index left through an index right, both included, where 0 <= left <= right < values.length. It may be one element or the whole array. It cannot wrap from the end back to the start.
The subarray must contain at least one element. So if every value is negative, the answer is the largest single value, even though it is negative.
Return only the largest sum. You do not need to return the subarray or its indices, so ties between equal sums do not matter.
Do not change values. Use fresh local state on each call. All sums and comparisons use exact integers.
Function
maxSubarraySum(values: int[]) → intExamples
Example 1
values = [-4,3,-2,5,-7,4]return = 6The contiguous subarray [3,-2,5] has sum 3-2+5 = 6. No contiguous subarray has a larger sum.
Example 2
values = [-7,-2,-5]return = -2The best nonempty subarray is the single value [-2], so the answer is -2. Choosing no elements is not allowed.
Example 3
values = [0,4,-1,3,-10,5,0]return = 6The subarray [4,-1,3] has sum 4-1+3 = 6. Adding the zero just before it gives the same sum. So the function returns 6.
Constraints
1 <= values.length <= 100.- Every element of
valuesis an integer between-1000000and1000000, inclusive. - The input and chosen subarray are both nonempty.
- Every interval sum has absolute value at most
100*1000000 = 100000000, which fits a signed 32-bit integer.
More Akuna Capital Phone Screen questions
- Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Biased Coin: Probability of Exactly h HeadsPHONE SCREEN · Seen Nov 2026
- Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Sep 2026
- Maximum Value in a Binary TreePHONE SCREEN · Seen Sep 2026
- Tiered Expiring Item StorePHONE SCREEN · Seen Aug 2026
- Statistics for the Latest K ValuesPHONE SCREEN · Seen Nov 2025
- Remove Duplicates While Preserving OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
- Sliding Window MaximumPHONE SCREEN · Seen Oct 2025
- Evaluate a PolynomialPHONE SCREEN · Seen Oct 2025
Similar questions at other companies
- Maximum Subarray Sum with One DeletionServiceNow · PHONE SCREENServiceNow interview questions
- Maximum Product of a Contiguous SubarrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Fewest Coins CombinationBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Longest Increasing SubsequenceByteDance · PHONE SCREENByteDance interview questions
- Jump GamePaytm · PHONE SCREENPaytm interview questions
- Query Words Within K PositionsPalantir · PHONE SCREENPalantir interview questions
- House RobberVisa · PHONE SCREENVisa interview questions
- Best Improvement Period of at Least K IntervalsAmazon · PHONE SCREEN
- Longest Increasing SubsequenceAngel One · PHONE SCREEN
- Match Shops and BuyersGoogle · PHONE SCREENGoogle interview questions
Akuna Capital interview experiences
- Akuna Capital Quant Trader and QR Interviews: 5 Experiences
- Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
- Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
- Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
- Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
- Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)