Reported August 2026
Infosysgreedy

Minimum Cost to Assign Candidates to Two Cities

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 piece of this problem that matters is a sorted list of cost differences, and that's what the Infosys OA reported in August 2026 is really testing. You get n candidates, two city prices each, and exactly half must go to each city. It looks like a DP or matching problem at first glance. It isn't. If your invite lands in the next day or two, this is a greedy sort with one idea behind it. StealthCoder sits invisibly as a safety net on the live OA if your mind goes blank, but the idea fits in one breath.

The problem

You are given an even-length matrix costs, where costs[i][0] is the cost of sending candidate i to City A and costs[i][1] is the cost of sending that candidate to City B.
Send every candidate to exactly one city, with exactly half of the candidates assigned to each city.
Return the minimum possible total assignment cost.

Function
minimumTwoCityCost(costs: int[][]) → long

Examples
Example 1
costs = [[10,20],[30,200],[400,50],[30,20]]
return = 110
Send candidates 0 and 3 to City A for 10 + 30, and candidates 1 and 2 to City B for 200 + 50 would cost 290. A cheaper valid split sends candidates 0 and 1 to City A and candidates 2 and 3 to City B, for 10 + 30 + 50 + 20 = 110.
Example 2
costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
return = 1859
One minimum-cost assignment sends candidates 0, 3, and 5 to City A, and candidates 1, 2, and 4 to City B.
Example 3
costs = [[1,100],[2,200]]
return = 102
Send the first candidate to City B and the second candidate to City A, for a total cost of 100 + 2 = 102.

Constraints
2 <= costs.length <= 100000
costs.length is even.
costs[i].length == 2
0 <= costs[i][j] <= 1000000000
The minimum total cost fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: start by sending everyone to City A, then pick which half to flip to City B. Flipping candidate i changes the total by costs[i][1] - costs[i][0]. You want the n/2 smallest changes, so sort by that difference and flip the first half. Equivalent form: sort by costs[i][0] - costs[i][1] and send the first half to A, the rest to B. Check it on Example 3: differences are 99 and 198, so the first candidate goes to B (100) and the second stays in A (2), total 102. Pitfalls: sorting by one city's cost alone, which fails the examples. Overflow is the other one. With 100000 candidates and values up to 1e9, the sum needs a 64-bit integer, so use long. Complexity is O(n log n) for the sort and O(1) extra beyond that. If you blank during the live OA, StealthCoder is the hedge that surfaces this sort-by-difference solution on screen without the proctor seeing it.

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 Minimum Cost to Assign Candidates to Two Cities 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

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as two city scheduling. If you have time before the OA, drill that.

⏵ 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.

Minimum Cost to Assign Candidates to Two Cities FAQ

What's the trick in Minimum Cost to Assign Candidates to Two Cities?+

Sort candidates by costs[i][0] - costs[i][1]. The first half has the biggest savings from going to City A, so they go there. The second half goes to City B. Sum the chosen costs. It's a greedy argument based on relative preference, not absolute price.

How hard is this Infosys OA question really?+

Easy to medium. The code is about ten lines once you see the difference-sort idea. The hard part is not reaching for DP. If you can explain why sorting by cost difference is optimal, you're done. Edge cases are minimal since the length is always even.

Why does the sum need a long?+

The function returns long because n can reach 100000 and each cost can reach 1e9. The total can reach about 1e14 in the worst case, which overflows a 32-bit int. Accumulate into a 64-bit variable from the start, not at the end.

Can I solve it with dynamic programming instead?+

Yes, with dp[i][j] meaning the best cost for the first i candidates with j sent to City A. But that's O(n^2), which is too slow for n = 100000. Greedy with a sort runs in O(n log n) and passes the constraints comfortably.

How do I prepare for this in 48 hours?+

Write the greedy solution from scratch once, then run all three examples by hand. Practice a second variant where the split isn't half and half, to see how the idea changes. Focus on custom comparator sorting in your language, since that's the part people fumble under time pressure.

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