Reported September 2026
Visaprefix sum

Optimal Transfer

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

Visa's September 2026 OA reportedly includes Optimal Transfer, and the trap is sitting in the setup. Servers are sorted by capacity, each has a unique closest neighbor, and that neighbor move costs a flat 1. Candidates who just sum capacity gaps or run Dijkstra per query will fail on time or on correctness. It's a greedy plus prefix sum problem in disguise. You need answers for up to 2*10^5 queries, so every query has to resolve in O(1). If you blank on the structure, StealthCoder can run invisibly as a safety net during the live assessment.

The problem

There are n servers in a network, arranged in ascending order of their capacities. The capacity of server i is capacity[i].
The distance between server i and server j is |capacity[i] - capacity[j]|. When n > 1, each server's closest server is the one with the smallest distance in capacity, and that closest server is guaranteed to be unique.
You may perform either of the following operations from a server x:
Connect directly to any server y at cost |capacity[x] - capacity[y]|.
When another server exists, connect to the closest server of x at a fixed cost of 1.
Given query arrays fromServer and toServer, compute the minimum cost required to connect from fromServer[i] to toServer[i] for every query. A connection may be direct or may route through intermediate servers. A query whose start and destination are equal costs 0.

Function
getMinCost(capacity: int[], fromServer: int[], toServer: int[]) → int[]

Examples
Example 1
capacity = [2,7,10]
fromServer = [0,1,2]
toServer = [2,2,1]
return = [2,1,1]
The closest-server moves are 0 -> 1, 1 -> 2, and 2 -> 1, each at cost 1. Therefore the optimal costs are 0 -> 1 -> 2 = 2, 1 -> 2 = 1, and 2 -> 1 = 1.
Example 2
capacity = [2,3,5,6]
fromServer = [0,2,0]
toServer = [3,0,1]
return = [4,3,1]
The closest-server moves are 0 -> 1, 1 -> 0, 2 -> 3, and 3 -> 2. The minimum costs are 1 + 2 + 1 = 4 from 0 to 3, 2 + 1 = 3 from 2 to 0, and 1 from 0 to 1.
Example 3
capacity = [1,4,9,15]
fromServer = [0,3,1]
toServer = [3,0,3]
return = [12,3,11]
The directed closest-server moves are 0 -> 1, 1 -> 0, 2 -> 1, and 3 -> 2. Walking through adjacent servers costs 12, 3, and 11 for the three queries.

Constraints
1 <= n, m <= 2 * 10^5.
1 <= capacity[i] <= 10^9.
capacity is strictly increasing.
When n > 1, every server has a unique closest server.
fromServer.length == toServer.length == m.
0 <= fromServer[i], toServer[i] < n.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Because capacities are sorted, the only useful moves are between adjacent servers. Walking from i to i+1 costs min(gap, 1) if i+1 is i's closest server, otherwise the plain gap. Walking right to left works the same way with its own closest check. Jumping far directly costs the sum of gaps anyway, so it never beats walking adjacent steps. Build two prefix arrays: one for rightward step costs, one for leftward step costs. Then each query is a prefix difference. The edge case that breaks naive solutions is direction. Closest-server moves are directed, so 0 to 1 can cost 1 while 1 to 0 costs a full gap, and you can't reuse one array for both. Also handle from equal to to as 0, and n equal to 1. Use 64-bit sums since gaps reach 10^9 across 2*10^5 servers. StealthCoder is the hedge if you freeze on the directional prefix setup mid-assessment.

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 Optimal Transfer 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.

Optimal Transfer FAQ

What's the trick in Visa's Optimal Transfer problem?+

Only adjacent moves matter because capacities are sorted. Compute the cost of each adjacent step, using 1 when the target is the closest server and the gap otherwise. Build prefix sums for both directions, then answer every query with a subtraction.

Why can't I just run Dijkstra per query?+

With n and m up to 2*10^5, per-query shortest path is far too slow. The sorted order means the graph collapses to a line, so prefix sums give each answer in O(1) after O(n) preprocessing.

What edge case breaks naive solutions?+

Direction. The closest-server shortcut is directed, so going left to right and right to left have different step costs. One shared prefix array gives wrong answers. Also check queries where from equals to, which cost 0.

How do I find each server's closest neighbor?+

Compare the gap to the left neighbor with the gap to the right neighbor. The smaller gap wins, and the problem guarantees no ties. The first server only has a right neighbor and the last only has a left neighbor.

How should I prepare for this in 48 hours?+

Practice prefix sums over a line with direction-dependent costs, and write the closest-neighbor computation by hand. Test on the three given examples, especially example 3 where costs are asymmetric. Use long integers for sums.

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