As reported: Microsoft SDE 2 (hiring drive, Nov 2025), round 2: an array
profits[]of sizenwhere a positive value is a profit month and a negative value a loss month; find the maximum sum of a contiguous subarray of length at mostk.
Given profits (positive = profit, negative = loss) and an integer k, return the maximum sum of a non-empty contiguous subarray whose length is at most k.
Example 1
profits = [3,-1,4,-10,5,2], k = 3 -> 7 [5,2]
Example 2
profits = [-4,-2,-7], k = 2 -> -2 [-2]
Example 3
profits = [1,2,3,4], k = 2 -> 7 [3,4]
Constraints: 1 ≤ n ≤ 10^5; 1 ≤ k ≤ n; -10^4 ≤ profits[i] ≤ 10^4.
Code runner format: one JSON value per line: profits, k. Implement maxSumAtMostK(profits, k); the harness prints the result.