FreeCareerPath
Build the PrimitiveMedium

Maximum Subarray

Write `max_subarray(nums)` returning the largest sum of any contiguous non-empty subarray. max_subarray([-2,1,-3,4,-1,2,1,-5,4]) -> 6 (4,-1,2,1) Kadane in one line of reasoning: the best subarray ending at i either extends the one ending at i-1 or starts fresh at i. A running sum that drops below the current element should be abandoned. Start the answer at the first element, not 0, or an all-negative input returns 0.

What to expect: A timer starts when you begin. Edit the starter code, run it against the test suite as many times as you like, then finish when you're done. The reference solution and interviewer follow-up questions unlock only after you finish.

Log in to start this session

Timed sessions and your results for "Maximum Subarray" are saved to your account — logging in takes a few seconds.

Maximum Subarray — FreeCareerPath