FastPrepMaximum Subarray
Problems/Maximum Subarray

Maximum Subarray

Microsoft logoMicrosoft● EasyNEW GRADINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report from a Microsoft Data Scientist PhD Intern asks for the maximum sum of a contiguous subarray. Our version follows the usual rule that the subarray is nonempty. We chose the function signature and the limits. Our version is about 95% the same as the reported question.

Given an integer array nums, return the largest possible sum of a nonempty contiguous subarray.

Function

maxSubArray(nums: int[]) → long

Examples

Example 1

nums = [-2,1,-3,4,-1,2,1,-5,4]return = 6

The subarray [4,-1,2,1] has the maximum sum 6.

Example 2

nums = [1]return = 1

The only nonempty subarray is the single element.

Example 3

nums = [-8,-3,-6,-2,-5,-4]return = -2

When 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

Interview reports that mention this question

  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft AI/ML Intern Interviews: 114 Experiences (Part 1 of 2)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public long maxSubArray(int[] nums) {
        // Write your code here.
    }

}
Checking account…