Minimum Spanning Tree Weight in a Complete Binary Graph
Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure that decides this one is union-find, or a set of unvisited nodes if you go the BFS route. Uber reported this OA in February 2026, and it looks like a graph problem until you notice the graph is complete and almost all edges weigh 0. You can't build the zero-weight graph, because n goes up to 2 * 10^5. So the real task is counting connected components of the complement graph. If you've got an invite and 48 hours, learn this trick once. StealthCoder is there as a safety net during the live OA if your mind goes blank on the complement idea.
The problem
You are given n nodes in a complete undirected graph. Some edges have weight 1; every other edge has weight 0. The weight-1 edges are given in the array oneEdges, where each edge is represented as [u, v]. Return the total weight of a minimum spanning tree of this complete graph. Function minimumSpanningTreeWeight(n: int, oneEdges: int[][]) → int Examples Example 1 n = 4 oneEdges = [[1, 2], [2, 3], [3, 4]] return = 0 All edges not listed in oneEdges have weight 0. The zero-weight edges can connect all four nodes, so the MST weight is 0. Example 2 n = 3 oneEdges = [[1, 2], [1, 3], [2, 3]] return = 2 Every edge has weight 1, so any spanning tree uses two weight-1 edges. Constraints 1 <= n <= 2 * 10^5 0 <= oneEdges.length <= min(n * (n - 1) / 2, 2 * 10^5) 1 <= u < v <= n for each edge in oneEdges oneEdges contains no duplicate edges.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The MST weight equals (number of connected components in the zero-weight graph) minus 1. Each component is joined internally by free edges, and you need one weight-1 edge to bridge each pair of components. Weight-1 edges are sparse, so the zero-weight graph is the dense complement. Do BFS on the complement: keep a set or linked list of unvisited nodes, pop one, and scan the unvisited set. A node moves to the queue only if it isn't a one-edge neighbor. Each node is removed once, and each one-edge causes at most a bounded number of failed checks, so it runs in about O(n + m). The pitfall is building an n by n adjacency matrix, which blows memory and time. Another is forgetting that nodes are 1-indexed. If you blank on the complement BFS in the live OA, StealthCoder can surface the approach while you keep your hands on the keyboard.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Minimum Spanning Tree Weight in a Complete Binary Graph 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Uber's OA.
Uber reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Spanning Tree Weight in a Complete Binary Graph FAQ
What's the trick in the Uber MST weight problem?+
Reframe it. Weight-0 edges are everything not in oneEdges, so the MST cost is just the number of connected components in that zero-weight graph minus 1. You never build the zero graph. You traverse it implicitly using an unvisited set.
How hard is this OA question really?+
Medium to hard. The code is short, but the insight about complement graph components isn't obvious. If you've seen complement BFS before, it's about 20 lines. If not, a brute-force MST will time out at n = 2 * 10^5.
Can I use Kruskal's or Prim's directly?+
Not on the full graph. A complete graph with 2 * 10^5 nodes has roughly 2 * 10^10 edges. You need to exploit that only up to 2 * 10^5 edges are weight 1, and count components of the complement instead of sorting edges.
How do I do complement BFS efficiently?+
Store oneEdges in a hash set per node. Keep a set of unvisited nodes. Pop a node from the queue, loop over the unvisited set, and move any node that isn't a one-edge neighbor into the queue. Skipped nodes stay. Total work stays near O(n + m).
How should I prepare in 48 hours?+
Write complement-graph BFS once from scratch, then test the two examples by hand. Also try n = 1 and an empty oneEdges list. Those edge cases return 0 and are easy to fumble. Keep the formula components - 1 in your head.