Code Room
CodingMediumcod-g007
Subject Minimum spanning treeLevel Mid–Senior~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

You must connect n cities (0..n-1) with cables. You are given a list of candidate cables as [u, v, cost]. Return the minimum total cost to connect all cities so that every city is reachable from every other. If it is impossible to connect them all, return -1. The graph is undirected; duplicate cables and self-cables (u == v) may appear.

Implement
min_cable_cost(n: int, cables: list[list[int]]) → int
Examples
in[4,[[0,1,1],[1,2,2],[2,3,3],[0,3,10]]]out6
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.