Reported October 2026
Visadynamic programming

Maximum Index-Weighted Sum with Disjoint Swaps

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

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

Strip the swap story away and this Visa OA question, reported in October 2026, is a matching problem on at most 18 positions. Each swap has a fixed gain, and you pick a set of non-overlapping swaps that maximizes total gain. The tag says union-find, but don't buy it. The tiny length cap points at bitmask dynamic programming. If you've got an invite and 48 hours, this is the shape to recognize. And if your mind goes blank mid-assessment, StealthCoder sits invisibly on your screen and hands you the approach while the proctor sees nothing.

The problem

For an integer array nums, its score is sum(i * nums[i]) using zero-based indices.
You may choose any number of pairwise-disjoint swaps, including zero. A swap may exchange any two positions, but no position may participate in more than one swap. Return the maximum attainable score.

Function
maxIndexWeightedSum(nums: int[]) → long

Examples
Example 1
nums = [2,1,4,3]
return = 20
Swapping positions 0 and 1 and positions 2 and 3 produces [1,2,3,4] with score 20.
Example 2
nums = [-1,5,2]
return = 12
Swapping positions 1 and 2 gives [-1,2,5], whose score is 12.

Constraints
1 <= nums.length <= 18.
-1000000 <= nums[i] <= 1000000.
The answer fits a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is the gain formula. Swapping positions i and j changes the score by (j-i)*(nums[i]-nums[j]). The base score is sum(i*nums[i]). Now choose disjoint pairs to maximize total gain. Greedy fails because taking the biggest swap can block two smaller ones that add up to more. Use dp over a mask of positions already used. Take the lowest unset bit i. Either leave it fixed and move on, or pair it with any unset j greater than i and add that gain. That's 2^18 states times 18 transitions, which is trivial. The common pitfall is overflow, so use 64-bit. Another is forgetting the option to skip a position. Union-find doesn't fit because pairings aren't transitive groups. If you freeze on the mask transition live, StealthCoder is the hedge that gives you the recurrence fast.

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 Index-Weighted Sum with Disjoint 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 Visa's OA.

Visa 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 Index-Weighted Sum with Disjoint Swaps FAQ

What's the actual trick in the Visa maximum index-weighted sum problem?+

Convert each possible swap into a gain, (j-i)*(nums[i]-nums[j]), then pick disjoint swaps to maximize total gain. With length at most 18, bitmask DP handles it. The state is which positions are already decided, and each step handles the lowest undecided position.

Why doesn't greedy work here?+

Picking the single best swap first can lock two positions that would've paired better elsewhere. Disjointness makes it a maximum weight matching problem. Greedy gives wrong answers on crafted inputs, so use DP over subsets, which is exact and cheap at n=18.

Is union-find the right pattern since the hint says so?+

No. Union-find merges elements into connected groups, but here each position can be in at most one swap, and pairs aren't transitive. The constraint of 18 elements is the real signal. Think bitmask dynamic programming, not connectivity.

What's the complexity and will it pass?+

There are 2^18 masks, about 262 thousand, and each does up to 18 transitions. That's around 4.7 million operations, which is nothing. Use a long for the score since values reach 1000000 times index times 18 summed. Memory for one array of longs is fine.

How do I prepare for this in 48 hours?+

Write the bitmask DP once from scratch. Compute the base score, define dp[mask] as the best extra gain, always process the lowest unset bit, and allow skip or pair. Test with both examples, expecting 20 and 12. Then try an all-negative array and a length-1 array for edge cases.

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

OA at Visa?
Invisible during screen share
Get it