This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 3: Search and graphs
Merge groups as connections arrive, and ask which group anything is in.
Builds on Graph BFS & DFS.
The question to ask
Am I merging things into groups as connections arrive, and asking whether two things share a group?
Union-find (disjoint set union) keeps a forest where each group has one root. find walks up to the root, union points one root at another. With path compression and union by size, both are effectively O(1). It shines when connections arrive one at a time and the questions are "are these two connected?", "how many groups are there?" or "which edge first created a cycle?". BFS answers those too, but would have to re-run after every edge.
Time O(α(n)) per operation, effectively constant · Space O(n)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Add edges in order. The first one whose ends already share a root closes a cycle. Nodes are 1-indexed here, so size the structure n + 1.
function findRedundantConnection(edges: number[][]): number[] {
const parent = Array.from({ length: edges.length + 1 }, (_, i) => i);
const find = (x: number): number => {
while (parent[x] !== x) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
};
for (const [a, b] of edges) {
const rootA = find(a);
const rootB = find(b);
if (rootA === rootB) return [a, b];
parent[rootA] = rootB;
}
return [];
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Given an adjacency matrix of cities, a province is a group of directly or indirectly connected cities. Return the number of provinces.
"Provinces" are "connected components": union every connected pair, read count. DFS works just as well here.
Not attempted yet
A tree had one extra edge added. Return the redundant edge that can be removed to make it a tree again, the last one in the input if several qualify.
"Redundant" edge: the first one whose ends already share a root.
Not attempted yet
Accounts with any email in common belong to the same person. Merge the accounts and return each person's emails sorted.
"Merge" accounts that "belong to the same" person through a shared email: union by email, then group by root.
Not attempted yet
A stone can be removed if another stone shares the same row or column. Return the most stones that can be removed.
"Same row or column" links stones into groups; each group can shrink to one stone, so the answer is stones minus groups.
Not attempted yet
Given equations like 'a==b' and 'b!=c' over single-letter variables, can every equation hold at once?
"Equations" of equality form groups: union every ==, then check no != joins two members of the same group.
Not attempted yet
Computers are joined by cables in a network. You may move a cable between any two computers. Return the minimum moves to connect them all, or -1.
Connecting a "network": you need components - 1 moves, and enough spare (redundant) cables to make them.
Not attempted yet
Given a string and index pairs that may be swapped any number of times, return the lexicographically smallest string possible.
Repeated "swaps" make each connected group of indices freely sortable: union the pairs, sort each group's letters.
Not attempted yet
You may change at most one 0 to 1 in a binary grid. Return the size of the largest island possible.
Label each island with its root and size, then for each 0 add up the distinct neighbouring roots plus one.
Not attempted yet