Magicsheet logo

Maximum Ascending Subarray Sum

Easy
59.9%
Updated 6/1/2025

Asked by 4 Companies

Topics

Maximum Ascending Subarray Sum

What is this problem about?

The Maximum Ascending Subarray Sum problem provides an array of positive integers. You need to find the maximum possible sum of a contiguous subarray where the elements are strictly ascending (meaning each element is strictly greater than the preceding one). For example, in [10, 20, 30, 5, 10, 50], the ascending subarrays are [10, 20, 30], [5, 10, 50]. You must return the highest sum among them.

Why is this asked in interviews?

This is a standard Easy-level screening question. Interviewers use it to assess basic coding fluency, loop mechanics, and state tracking. It ensures a candidate can differentiate between a "subsequence" (can skip elements) and a "subarray" (must be contiguous) and can properly reset running totals when a condition is broken.

Algorithmic pattern used

This problem relies on a Single Pass / Sliding Window pattern. You maintain a current_sum and a max_sum. You iterate through the array starting from index 0. If the current element is strictly greater than the previous element, you add it to current_sum. If it is less than or equal to the previous element, the ascending chain is broken. You update max_sum, and reset current_sum to the value of the current element (because it starts a new chain).

Example explanation

Array: [10, 20, 30, 5, 10, 50] Initialize: curr_sum = 10 (first element), max_sum = 10.

  • Index 1 (20): 20>1020 > 10. Ascending! curr_sum = 10 + 20 = 30. max_sum = 30.
  • Index 2 (30): 30>2030 > 20. Ascending! curr_sum = 30 + 30 = 60. max_sum = 60.
  • Index 3 (5): 55 is NOT >30> 30. Chain breaks! Update max_sum = max(60, 60) = 60. Reset curr_sum = 5.
  • Index 4 (10): 10>510 > 5. Ascending! curr_sum = 5 + 10 = 15.
  • Index 5 (50): 50>1050 > 10. Ascending! curr_sum = 15 + 50 = 65. max_sum = max(60, 65) = 65. The maximum ascending subarray sum is 65.

Common mistakes candidates make

A common error for beginners is resetting the curr_sum to 0 when the chain breaks. If you hit a number that breaks the chain (like the 5 in the example), that 5 is actually the first element of the next potential chain! You must reset the curr_sum to arr[i], not 0. Another mistake is forgetting to do a final max_sum = Math.max(max_sum, curr_sum) check after the loop ends to account for arrays that are entirely ascending.

Interview preparation tip

For the Maximum Ascending Subarray Sum interview question, you can avoid the "post-loop check" bug entirely by updating your max_sum inside every iteration of the loop, rather than only when the chain breaks. Doing max_sum = Math.max(max_sum, curr_sum); universally at the bottom of your for loop keeps the code exceptionally clean and bug-proof.

Similar Questions