Reported February 2026
Infosysprefix sum

Maximum Subarray Sum After Swaps

Reported by candidates from Infosys's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Infosys OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt at this Infosys OA question, reported in February 2026, is treating it like plain Kadane with a swap bolted on. You're given an array and up to k swaps of any two indices, and you want the best subarray sum. The hinted pattern is prefix-sum, and with n up to 500 you can afford to enumerate every subarray. If you've got the OA in a day or two, lock in the idea before you code. StealthCoder is the safety net if you blank mid-assessment, but the trick below is short enough to remember.

The problem

You are given an integer array a and an integer k.
You may perform at most k swap operations. In one swap, choose any two indices i and j and swap a[i] with a[j].
After performing at most k swaps, return the maximum possible subarray sum.

Function
maximumSubarraySumAfterSwaps(a: int[], k: int) → int

Examples
Example 1
a = [1,-5,2]
k = 1
return = 3
Choose the subarray [-5,2] and swap -5 with the outside value 1. The resulting chosen subarray has sum 1 + 2 = 3.
Example 2
a = [4,-10,3,2]
k = 1
return = 9
Choose the subarray [4,-10,3] and swap -10 with the outside value 2. The subarray sum becomes 4 + 2 + 3 = 9.
Example 3
a = [-2,3,-1]
k = 0
return = 3
No swaps are allowed, so this is the usual maximum subarray sum.

Constraints
2 <= a.length <= 500
0 <= k <= 500
-1000 <= a[i] <= 1000

Reported by candidates. Source: FastPrep

Pattern and pitfall

Fix a subarray [l, r]. Swaps can pull any outside element in and push any inside element out. So for that window, take the inside elements sorted ascending and the outside elements sorted descending. Swap the smallest inside with the largest outside, up to k times, and only while the outside value is strictly bigger. Prefix sums give you the window sum fast. At n = 500 that's about 125k windows, and sorting each is fine, roughly n^3 log n worst case but workable. The common pitfall is greedy-swapping globally or running Kadane first and then swapping. The best window changes once swaps are allowed. Another trap is swapping when it makes the sum worse. Stop at the first non-improving swap. Example 2 shows it: window [4,-10,3] swaps -10 for the outside 2 and gives 9. If the live OA freezes you on the window logic, StealthCoder can hand you the loop.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Maximum Subarray Sum After Swaps cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Infosys's OA.

Infosys reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Subarray Sum After Swaps FAQ

What's the trick in Maximum Subarray Sum After Swaps?+

Enumerate every window. For each one, swap its smallest inside values with the largest outside values, at most k times, only when it helps. Prefix sums give the base window sum. Track the best total across all windows.

Can I just run Kadane and then apply swaps?+

No. That's the classic first-attempt failure. The window that's best without swaps often isn't the best with swaps, because a bad element inside a different window can be replaced by a big outside value. You have to evaluate swaps per window.

Will brute force pass with a length of 500?+

Enumerating all windows is about 125k, and sorting the inside and outside parts per window is manageable at this size. You can optimize with a heap or by keeping sorted lists, but the plain approach is the safe one to write first.

What edge cases should I test?+

Test k = 0, which must match plain maximum subarray sum. Test all-negative arrays, where swaps may not help. Test k larger than the window size. Test windows where the outside value is smaller than the inside one, so you correctly skip the swap.

How do I prep for this in 48 hours?+

Rewrite Kadane and prefix sums from memory. Then hand-trace Example 2 on paper using the sort-and-swap-per-window idea. Code it once, cleanly, and test k = 0 and all-negative inputs. That's enough for this Infosys question.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Infosys.

OA at Infosys?
Invisible during screen share
Get it