Two City Scheduling
A medium-tier problem at 68% community acceptance, tagged with Array, Greedy, Sorting. Reported in interviews at Swiggy and 1 others.
Two City Scheduling lands on a lot of OAs, and candidates either nail it in under five minutes or spin their wheels on a brute-force approach that times out. You're given costs for sending each person to one of two cities, and you need to minimize total cost. The trick isn't dynamic programming or recursion. It's a single insight about sorting that makes the optimal assignment obvious. Swiggy and Bloomberg have both asked this. If you haven't drilled the greedy pattern and hit it live, StealthCoder surfaces the solution invisible to the proctor so you don't blow easy points.
Companies that ask "Two City Scheduling"
Two City Scheduling is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who realized the OA tests how well you memorized 200 problems, not how well you code.
Get StealthCoderThe trap is thinking you need to try all possible splits or use DP. Instead, compute the cost difference for each person (city A minus city B) and sort by that difference. Send the people with the lowest differences to city A and the rest to city B. That's it. Why does this work? Sorting the differences isolates which assignments matter most. You're greedily picking the assignments where city A has the biggest advantage, then defaulting everyone else to city B. Common pitfalls: building a graph, overthinking state transitions, or writing recursive backtracking. The acceptance rate is solid at 67%, so most people get there eventually, but many waste time on the harder mental model first. StealthCoder handles the moment you realize your current approach isn't working and you need the pattern delivered immediately.
Pattern tags
You know the problem.
Make sure you actually pass it.
Two City Scheduling recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who realized the OA tests how well you memorized 200 problems, not how well you code. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Two City Scheduling interview FAQ
Is Two City Scheduling actually a greedy problem or does it need DP?+
Pure greedy. Sort by cost difference and assign based on that single metric. DP or recursion is overkill and wastes time. The acceptance rate of 67% includes people who do it the hard way first then optimize. Greedy is the intended path.
Do Swiggy and Bloomberg really ask this or is it just in one hiring cycle?+
Both companies have reported asking it. It's stable enough on the interview circuit that you can't skip it. The problem appears frequently enough that it's worth drilling the greedy insight until it's automatic.
What's the actual trick I'm missing if my brute-force solution times out?+
You're probably trying all subsets or permutations. The trick: sort candidates by the difference in cost between city A and city B, then assign the first half to city A and the second half to city B. One pass, one sort, O(n log n). That's the entire algorithm.
How does sorting relate to the greedy choice here?+
Sorting the cost differences ranks which people benefit most from city A relative to city B. Greedy means you lock in the best deals first. Once sorted, the assignment is deterministic. No backtracking needed.
If I only have time to drill one array and sorting problem before my OA, should I pick this?+
Yes. It's medium difficulty, asked by real companies, and the greedy pattern generalizes to other scheduling and allocation problems. Sixty-seven percent acceptance means it's accessible but people still miss the insight under pressure.
Want the actual problem statement? View "Two City Scheduling" on LeetCode →