Minimum Cost for Roads and Hospitals
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Amazon OA from September 2026 dresses up a graph problem as city planning. It's "Minimum Cost for Roads and Hospitals," and once you strip the story it's connected components plus one cost comparison. If you're taking it in the next day or two, that's the whole game. Build hospitals, build roads, make sure every city reaches a hospital. Sounds like optimization hell, but it isn't. Each component gets one hospital, and the only question is how you connect it. StealthCoder sits invisibly on your screen as a safety net if you blank during the live OA, but you should be able to write this one yourself.
The problem
Cities are labeled from 0 to cityCount - 1. A hospital can be built in any city for hospitalCost, and any listed undirected road can be built for roadCost. Choose hospitals and roads at minimum total cost so every city can reach a hospital. Function minimumInfrastructureCost(cityCount: int, possibleRoads: int[][], hospitalCost: int, roadCost: int) → int Examples Example 1 cityCount = 3 possibleRoads = [[0,1],[1,2]] hospitalCost = 3 roadCost = 2 return = 7 Case 1 exercises the documented deterministic contract. Example 2 cityCount = 3 possibleRoads = [[0,1]] hospitalCost = 2 roadCost = 5 return = 6 Case 2 exercises the documented deterministic contract. Example 3 cityCount = 5 possibleRoads = [[0,1],[2,3]] hospitalCost = 6 roadCost = 1 return = 20 Case 3 exercises the documented deterministic contract. Constraints 1 <= cityCount <= 200000. 0 <= possibleRoads.length <= 200000. Costs are positive and the answer fits a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's what it reduces to: every connected component needs exactly one hospital. Inside a component of size k, you can connect all cities with k-1 roads (a spanning tree), or skip roads and build a hospital in each city for k * hospitalCost. So per component, cost is hospitalCost + (k-1) * min(roadCost, hospitalCost). Sum across components. Check example 3: components sizes 2, 2, 1. Hospital 6, road 1. Two pairs cost 6+1 = 7 each, the singleton costs 6. Total 20. Correct. Use union-find or iterative BFS, since cityCount reaches 200000 and recursive DFS can overflow the stack. The common pitfall is always building roads even when roadCost exceeds hospitalCost, as in example 2. Another is forgetting isolated cities, which are their own components. If you freeze in the live OA, StealthCoder can hand you the union-find skeleton, but the formula is the real trick.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Minimum Cost for Roads and Hospitals 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Cost for Roads and Hospitals FAQ
What's the trick in Minimum Cost for Roads and Hospitals?+
Split the cities into connected components. Each component needs exactly one hospital. For the rest of its k cities, pay the cheaper of a road or a hospital. Cost per component is hospitalCost + (k-1) * min(roadCost, hospitalCost). Sum all components.
Which data structure should I use for this Amazon OA?+
Union-find with path compression is the cleanest. Union each road's endpoints, then count the size of each root. BFS also works, but go iterative. With 200000 cities, recursive DFS risks a stack overflow in some languages.
Why does example 2 return 6 and not something with a road?+
Cities 0 and 1 are linked by a possible road, city 2 is isolated. Road cost 5 is higher than hospital cost 2, so building a hospital in each city is cheaper. Three hospitals at 2 each gives 6. That's the min comparison in action.
How hard is this problem really?+
Easy to medium. The code is short, but the reduction is the hard part. If you spot components and the road-versus-hospital comparison, it's about 20 lines. Most failures come from missing the min check or ignoring isolated cities.
How do I prepare in 48 hours for this kind of problem?+
Write union-find from memory until it takes five minutes. Then solve a couple of connected-components counting problems and practice per-component cost formulas. Test on edge cases: no roads, one city, and roadCost larger than hospitalCost. Watch for integer overflow with large sums.