← Questions

Uniformly random spanning tree over node groups

0
DatabricksCodingLast asked May 2026

A coding interview question asked in Databricks's technical phone screen, last asked May 2026. It tests graphs, randomized and union-find.

You are given groups, a list of K disjoint, non-empty lists of integer node IDs. Nodes inside a group are already connected to each other; different groups are not connected.

You may add undirected edges between nodes of different groups. A set of new edges is valid if it connects all groups using the minimum number of edges, which is exactly K - 1. Equivalently, if you shrink each group to one vertex, the new edges form a spanning tree over the groups (no cycles, nothing left out).

Part 1. Implement connect_groups(groups) -> list[tuple[int, int]] that returns any valid set of edges.

Part 2. Implement sample_edges(groups, rng) -> list[tuple[int, int]] that returns a valid set chosen uniformly at random among all valid sets: every distinct set of K - 1 edges (edge (a, b) is the same as (b, a), order of edges does not matter) must have the same probability. Explain why your sampler is uniform and how you would test it.

Constraints

  • 1 <= K <= 10^5, total number of nodes N <= 10^6.
  • Node IDs are unique across all groups.
  • If K = 1, return an empty list.

Example 1

groups = [[10, 11], [20]]

Valid answers: [(10, 20)] and [(11, 20)]. Part 1 may return either. Part 2 must return each with probability 1/2.

Example 2

groups = [[1], [2], [3], [4]]

There are 16 valid answers (every spanning tree on 4 labelled vertices): 4 stars such as [(1, 2), (1, 3), (1, 4)] and 12 paths such as [(2, 1), (1, 4), (4, 3)]. Part 2 must return each of the 16 with probability 1/16. Adding random cross-group edges until everything is connected does not achieve this.

Clarifying questions

  • Is "uniform" over sets of node-to-node edges, so a group with more nodes takes part in more of them?
  • Is rng a random.Random-like object, and which methods may I use?
  • Can a group's node list contain duplicates?
  • For Part 1, does any spanning tree do, such as a chain of the groups in order?

What a strong answer covers

  • Part 1 links the groups in a chain: one node of group i to one node of group i + 1. That gives K - 1 edges and connects everything.
  • Part 2 sees that a valid edge set is a spanning tree of the complete multipartite graph, so a random walk method such as Aldous-Broder or Wilson's algorithm samples it uniformly. Each step picks a random node outside the current group, with probability proportional to how many nodes there are, and the first entry into a group adds the edge used.
  • Explains why it is uniform: these algorithms sample a uniform spanning tree of any graph, and a valid set here is exactly a spanning tree of the graph whose edges join nodes of different groups.
  • States the cost and how to keep it near O(N + K) in expectation for most inputs. Sampling a random node outside a group can use one random index into all nodes, with a retry when it lands in the same group.
  • Tests: Example 1 with a frequency count over many runs, Example 2 checked with a chi-squared test over all 16 trees, K = 1, and one group much larger than the others.

Follow-ups

  • Count the number of valid sets (a closed form exists for complete multipartite graphs, or use the matrix-tree theorem).
  • Give edges weights and sample a tree with probability proportional to the product of its weights.
  • Sample a uniform spanning tree of an arbitrary given graph, not just between groups.
  • Sample many trees fast for a test of a network layout.

Where it's asked

What it tests

graphs · randomized · union-find · combinatorics

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding