FastPrepWater Supply With Wells and Pipes
Problems/Water Supply With Wells and Pipes

Water Supply With Wells and Pipes

Nutanix logoNutanix● HardINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2021 report from a virtual Nutanix campus loop for an MTS intern role describes this as the last question of the live DSA round. Every house in a town needed water. Each house had its own price for a well. A graph of two-way links with prices could carry water between houses. The goal was the cheapest setup that reaches every house. The interviewer only asked for the idea, pseudo-code and running time.

The report gives no input format, limits or return value. Here houses are labeled 1 to n, and well prices come as a list. Each pipe comes as [a, b, cost] between two different houses. Every price is a whole number from 1 to 10^5. There are at most 10^4 houses and at most 10^4 pipes. Two houses can share more than one pipe. The pipes do not have to reach every house. You return only the lowest total cost. You also write and run full code instead of pseudo-code.

Our version is about 90% the same as the reported question.

A town has n houses, labeled from 1 to n, and every house needs water. There are two ways to bring it in:

  • Build a well at house i. This costs wellCost[i - 1].
  • Lay a pipe from the list pipes. Each pipes[j] = [a, b, c] is a pipe between house a and house b that costs c. Water can flow through a pipe in both directions.

A house has water if it has its own well, or if built pipes join it, directly or through other houses, to a house with a well. You may build any number of wells and lay any of the pipes.

Return the smallest total cost that gives water to every house.

Function

minCostToSupplyWater(n: int, wellCost: int[], pipes: int[][]) → int

Examples

Example 1

n = 4wellCost = [5,3,6,8]pipes = [[1,2,2],[2,3,4],[3,4,1],[1,4,7]]return = 10

Build one well at house 2 for 3, then lay the pipes 1-2 for 2, 2-3 for 4 and 3-4 for 1. Every house is now joined to house 2, so the total is 3 + 2 + 4 + 1 = 10.

Swapping any part for something else costs more. For example, a second well at house 3 (cost 6) instead of the pipe 2-3 (cost 4) gives 12.

Example 2

n = 3wellCost = [2,9,2]pipes = [[1,2,3],[2,3,3],[1,3,10]]return = 7

Wells at houses 1 and 3 cost 2 each. House 2 then takes the pipe 1-2 for 3, so the total is 2 + 2 + 3 = 7. The pipe 2-3 would work just as well and gives the same total.

One well is not enough here: a single well at house 1 needs the pipes 1-2 and 2-3, which costs 2 + 3 + 3 = 8.

Example 3

n = 5wellCost = [4,4,4,1,10]pipes = [[1,2,1],[2,3,1],[4,5,2]]return = 9

No pipe joins houses 1, 2 and 3 to houses 4 and 5, so each group needs its own well.

Houses 1, 2 and 3: one well for 4 plus the pipes 1-2 and 2-3 for 1 each, which is 6. Houses 4 and 5: a well at house 4 for 1 plus the pipe 4-5 for 2, which is 3. The total is 6 + 3 = 9.

Constraints

  • 1 <= n <= 10^4
  • wellCost.length == n
  • 1 <= wellCost[i] <= 10^5
  • 0 <= pipes.length <= 10^4
  • pipes[j].length == 3
  • 1 <= pipes[j][0], pipes[j][1] <= n
  • pipes[j][0] != pipes[j][1]
  • 1 <= pipes[j][2] <= 10^5
  • The same two houses can be joined by more than one pipe, and the pipes do not have to join all the houses.

More Nutanix Onsite Interview questions

  • Design Hit CounterONSITE INTERVIEW · Seen Oct 2026
  • Peak Process MemoryONSITE INTERVIEW · Seen Aug 2026
  • Minimum Operations to Transform ArrayONSITE INTERVIEW · Seen Jun 2026
  • Count Connected Components in an Undirected GraphONSITE INTERVIEW · Seen Aug 2025
  • LRU CacheONSITE INTERVIEW · Seen Aug 2025
  • Running Median of a Multilevel Linked List in Level OrderONSITE INTERVIEW · Seen Aug 2025
  • Custom Array IteratorONSITE INTERVIEW · Seen Dec 2024
  • Collapse Runs of Repeated CharactersONSITE INTERVIEW · Seen Nov 2022
  • Anagram Words in a SentenceONSITE INTERVIEW · Seen Apr 2022
  • Sudoku SolverONSITE INTERVIEW · Seen Feb 2022

Similar questions at other companies

  • Maximum Edge Removals for Two TravelersMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
  • Minimum Cost for Roads and HospitalsAmazon · ONSITE INTERVIEW
  • Top-K Affinity Nodes in a Connected ComponentGoogle · ONSITE INTERVIEWGoogle interview questions
  • Minimum Cost to Supply Water with Wells and PipesInfosys · OAInfosys interview questions
  • Maximum Sum of a K-StarMicrosoft · OAMicrosoft interview questions
  • Maximum K-Star SumC3.ai · OAC3.ai interview questions
  • Doctor Appointment Slot AssignmentDRW · OADRW interview questions
  • Scheduled Payments with Account AliasesCoinbase · ONSITE INTERVIEWCoinbase interview questions
  • Minimum Refueling StopsUber · ONSITE INTERVIEWUber interview questions
  • Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions

Interview reports that mention this question

  • Nutanix Intern Debugging Round and Interviews: 25 Experiences

Nutanix interview experiences

  • Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)
  • Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)
  • Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)
  • Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
All Nutanix interview questionsSee Nutanix hiring insights
Saved
class Solution {
    public int minCostToSupplyWater(int n, int[] wellCost, int[][] pipes) {
        // Write your code here.
    }
}
Checking account…