Maximize the Sum of a Nonempty Circular Array Segment
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Treat the end of `nums` as adjacent to its beginning. Return the largest sum of a nonempty contiguous circular segment, using each array position at most once. A segment may wrap across the end but cannot make a second lap.
```cpp
class Solution {
public:
int maxSubarraySumCircular(vector<int>& nums);
};
```
### Examples
**Example 1:**
```text
Input: nums = [8,-4,7]
Output: 15
Explanation: The wrapping segment[7,8] skips-4 and has sum15.
```
**Example 2:**
```text
Input: nums = [-5,-1,-2]
Output: -1
Explanation: A nonempty segment is required; the singleton-1 is best.
```
### Constraints
- `n == nums.length`; `1 <= n <= 3*10^4`.
- `-3*10^4 <= nums[i] <= 3*10^4`.
### Contract considerations
The returned answer is a sum, and a length-n segment is allowed exactly once around the buffer.
```hint Read the boundary condition
What contiguous interval would be omitted by a wrapping segment?
```
### Boundary reminders
The returned answer is a sum, and a length-n segment is allowed exactly once around the buffer.
Overview: Maximize the Sum of a Nonempty Circular Array Segment. Treat the end of nums as adjacent to its beginning.
Maximize the Sum of a Nonempty Circular Array Segment
Microsoft
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
Treat the end of nums as adjacent to its beginning. Return the largest sum of a nonempty contiguous circular segment, using each array position at most once. A segment may wrap across the end but cannot make a second lap.
class Solution {
public:
int maxSubarraySumCircular(vector<int>& nums);
};
Examples
Example 1:
Input: nums = [8,-4,7]
Output: 15
Explanation: The wrapping segment[7,8] skips-4 and has sum15.
Example 2:
Input: nums = [-5,-1,-2]
Output: -1
Explanation: A nonempty segment is required; the singleton-1 is best.
Constraints
n == nums.length
;
1 <= n <= 3*10^4
.
-3*10^4 <= nums[i] <= 3*10^4
.
Contract considerations
The returned answer is a sum, and a length-n segment is allowed exactly once around the buffer.
Boundary reminders
The returned answer is a sum, and a length-n segment is allowed exactly once around the buffer.