FastPrepMinimum Cost for Roads and Hospitals
Problems/Minimum Cost for Roads and Hospitals

Minimum Cost for Roads and Hospitals

Amazon logoAmazon● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The reported task identifies this algorithmic exercise. The executable interface, bounds, deterministic tie rules, and examples are the documented practice contract.

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 = 3possibleRoads = [[0,1],[1,2]]hospitalCost = 3roadCost = 2return = 7

Case 1 exercises the documented deterministic contract.

Example 2

cityCount = 3possibleRoads = [[0,1]]hospitalCost = 2roadCost = 5return = 6

Case 2 exercises the documented deterministic contract.

Example 3

cityCount = 5possibleRoads = [[0,1],[2,3]]hospitalCost = 6roadCost = 1return = 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.

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public int minimumInfrastructureCost(int cityCount, int[][] possibleRoads, int hospitalCost, int roadCost) {
        // Write your code here.
    }

}
Checking account…