The Possible Bipartition problem gives you a list of dislikes between people (they can't be in the same group) and asks whether it's possible to divide everyone into two groups with no dislikes within a group. This is equivalent to checking if the dislikes graph is bipartite. The union find, BFS, graph, and DFS interview pattern is the core.
Samsung, Uber, Microsoft, Meta, Amazon, TikTok, Google, and Bloomberg ask this because bipartite graph checking is a fundamental graph algorithm used in assignment problems, scheduling, and social network analysis. Both BFS/DFS coloring and Union-Find approaches are valid.
BFS/DFS graph coloring. Build the dislikes graph as an adjacency list. BFS/DFS: assign color 0 to the first node of each component. For each neighbor: if not colored, assign opposite color; if colored same as current, return false (odd cycle detected). If all nodes are 2-colorable, return true.
n=4, dislikes=[[1,2],[1,3],[2,4]].
n=3, dislikes=[[1,2],[1,3],[2,3]]: triangle (odd cycle). Color 1=0,2=1,3=1. Check 2-3: both color 1 → conflict. Return false.
Possible Bipartition and "Is Graph Bipartite?" are the same problem. Bipartite checking = 2-colorable = no odd cycles. BFS coloring is clean: color alternately, detect same-color neighbors. The Union-Find approach: for each pair (a, b) with a disliking b, union a with all of b's dislikes and vice versa. Practice both approaches until fluent — they're asked separately at different companies.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Is Graph Bipartite? | Medium | Solve | |
| Redundant Connection | Medium | Solve | |
| Graph Valid Tree | Medium | Solve | |
| Number of Provinces | Medium | Solve | |
| Number of Connected Components in an Undirected Graph | Medium | Solve |