Code Room
CodingHardcod-g513
Subject MstLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

You must supply water to n cities (1..n). For each city i you can either build a well in it at cost wells[i-1], or lay a bidirectional pipe between two cities given as [a, b, cost] to share water. A city has water if it has its own well or is connected by pipes to a city that has one. Return the minimum total cost to bring water to every city. wells has length n; there can be multiple pipes between the same pair.

Implement
min_water_cost(n: int, wells: list[int], pipes: list[list[int]]) → int
Examples
in[3,[1,2,2],[[1,2,1],[2,3,1]]]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.