Three interview problems about groups that link up. The move: build the nodes, then read the answer from the group count: nodes minus merges.
You work on a ride-hailing app, an ad platform and a data-hall monitor. The three problems on this street ask about groups. Two items are in the same group when a chain of links joins them. The question is how many groups there are, or whether the links make exactly one.
What counts as a link is different in each problem.
| Problem | Two nodes are linked when | What the answer reads |
|---|---|---|
| Driver pools that share zones | two drivers covered a common zone | the number of groups |
| Strictest overlap for k audience clusters | their overlap is at least the threshold | the largest threshold with at most k groups |
| Cables to remove for a tree | a cable joins two servers | the cables on the one cycle, if the network is connected |
Read the middle column. In the first problem the links are not listed: you have to build them. In the second there is a different set of links for every threshold. In the third they are listed, and may repeat.
You know how to count groups from the Academy: start a walk from every node nobody reached yet. This street counts groups with a simpler tool, and then reads more from the count.
Exercise · Predict the output: How many groups remain?. Six nodes are numbered 0 to 5. The code starts with six groups and subtracts one for every link that joins two different groups. What does it print?
n = 6
links = [[0, 1], [1, 2], [0, 2], [3, 4], [2, 1]]
parent = list(range(n))
groups = n
for a, b in links:
while parent[a] != a:
a = parent[a]
while parent[b] != b:
b = parent[b]
if a != b:
parent[a] = b
groups -= 1
print(groups)Make three decisions first. Name the nodes, which are the things that can be in a group. Decide what a link is. Then start from one group for each node and merge.
Every link does one of two things. It joins two different groups, which lowers the count by one, or it joins two nodes of the same group, which changes nothing and closes a cycle. That one fact answers four questions.
| The question | What you read from the merges |
|---|---|
| How many groups? | n minus the number of merges |
| Is it one group? | exactly n - 1 merges |
| Is there a cycle? | any link that was not a merge |
| How many independent cycles? | m - n + c, the links that were not merges |
Each link either merges two groups or closes a cycle inside one. So the groups number n minus the merges, and a tree has n - 1 merges and no wasted link.
A walk from every unseen node gives the same number. The merge count is shorter to write, and it also tells you about the cycles.
Count the groups as nodes minus merges: a link between two groups lowers the count, a link inside one group does not.
Exercise · Find the bug: No cycle is not enough. This should return true when the links make a tree on n servers. It rejects every cycle. Which line lets a wrong network through?
def is_valid_tree(n, links):
parent = list(range(n))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
merged = 0
for a, b in links:
ra, rb = find(a), find(b)
if ra == rb:
return False
parent[ra] = rb
merged += 1
return TrueThe Academy lessons BFS and DFS on any graph and Cycles, colouring and components count components and test for a tree. Union-find makes each merge almost constant. This street changes three things.
The graph is hidden. In the pool problem each driver lists zones, not other drivers.
Linking every pair who share a zone is slow: 1,000 drivers on one zone make 499,500 links. Link each driver to the first driver who covered the zone instead. That is 999 links and the same groups.
The links depend on a number. The overlap problem gives a matrix, and a link exists when the overlap is at least a threshold t. Take four campaigns.
overlap 0 1 2 3
0 - 70 20 10
1 70 - 40 5
2 20 40 - 60
3 10 5 60 -At t = 40 the four campaigns form 1 cluster. At t = 60 they form 2, and at t = 71 they form 4. A higher t only removes links, so the count never goes down. For k = 2 the answer is 60.
That lets you binary search t from 0 to 1,000,000 in 20 steps, instead of trying 1,000,001 values.
The count is also a test. With n nodes, m links and c groups there are m - n + c independent cycles. The cables problem has n cables for n servers.
If the network is connected, that is exactly one cycle, and removing any cable on it leaves a tree. With 6 servers and a loop through servers 1, 2 and 3, three cables qualify.
Find the loop by peeling: remove any node with one cable, again and again, and what is left is the cycle.
Take 2,000 servers with a loop of 1,000 and a tail of 1,000. Removing each cable in turn and testing costs 2,999,000 link checks. Peeling costs 5,999 visits, and both find the same 1,000 cables.
Exercise · Break it: The node nobody links to. This counts the groups of n nodes. It merges the linked nodes, then counts the different roots among the nodes that appear in a link. Find an input where it gives the wrong count.
def count_groups(n, links):
parent = list(range(n))
def find(x):
while parent[x] != x:
x = parent[x]
return x
for a, b in links:
ra, rb = find(a), find(b)
if ra != rb:
parent[ra] = rb
return len({find(x) for pair in links for x in pair})Count the roots of all n nodes, not only the nodes that appear in a link, or the nodes with no link vanish.
Solve them in this order. The first builds the links, the second makes the links depend on a number, and the third reads a cycle out of the count.
Before you code each problem, write the three decisions: the nodes, what a link is and what you read from the merges. If you cannot say what a node is, you will build the wrong graph.
Give each problem about 25 minutes. Spend the first five on a small example with a node that has no link and a link that repeats.
Then write three small tests: no links at all, one link repeated twice, and a graph that is one long chain. These show whether you count isolated nodes and whether a repeated link is treated as a cycle.
When an answer is wrong, count by hand with the merge rule on the failing input. Most wrong answers on this street come from a missing node or from a link that was built in the wrong direction.
Checkpoint · Connected components
Q1/5 There are 8 nodes and 5 links, and 4 of the links join two different groups. How many groups are there?
Answer: B
Start with 8 groups and subtract one for each merge: 8 - 4 = 4. The fifth link joined two nodes already in one group, so it changes nothing.
Q2/5 There are 10 nodes and 9 links, and every link merges two different groups. What do you know?
Answer: B
10 - 9 = 1 group, and no link was wasted inside a group, so no link closed a cycle.
Q3/5 A graph has 12 nodes, 14 links and 2 groups. How many independent cycles does it have?
Answer: B
The number is m - n + c = 14 - 12 + 2 = 4.
Q4/5 Drivers list the zones they covered. Two drivers are in one pool when they share a zone. Why not link every pair of drivers who share a zone?
Answer: B
Both ways give the same groups. The first-driver links are enough to join everyone who shares a zone.
Q5/5 Why can the overlap problem binary search the threshold?
Answer: C
Because the count is monotone, you can find the largest threshold that still gives at most k clusters in 20 steps over 0 to 1,000,000.
Review cards from this lesson.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.