886. Possible Bipartition
Asked at TikTok, Snowflake
Difficulty
Medium
Time
O(n + e)
Space
O(n + e)
Pattern
Depth-First Search
Companies
2
Problem
You have n people, numbered 1 to n. Some pairs of people dislike each other. You must put every person into one of two groups so that no two people who dislike each other land in the same group. Groups may have any size, including zero.
You are given the number n and a list dislikes, where each entry [a, b] means person a and person b dislike each other. Return true if such a split exists, otherwise return false.
Think of people as nodes and each dislike pair as an edge between them. The question becomes: can we paint every node with one of two colors so that the two ends of every edge get different colors? This is the classic bipartite graph check. A graph like this works if and only if it has no cycle of odd length.
Two groups, no dislike inside a group
Constraints
- ●
1 <= n <= 2000 - ●
0 <= dislikes.length <= 10^4 - ●
dislikes[i].length == 2 - ●
1 <= ai < bi <= n - ●
All pairs in dislikes are unique
Examples
n = 4, dislikes = [[1,2],[1,3],[2,4]]truen = 3, dislikes = [[1,2],[1,3],[2,3]]falseAsked At
2 companies| Company | Difficulty | |
|---|---|---|
| TikTok | Medium | View all TikTok questions → |
| Snowflake | Medium | View all Snowflake questions → |
How to Think About It
Model people as nodes and each dislike pair as an undirected edge. Splitting into two groups with no internal edges is exactly a 2-coloring of the graph.
Key insight: BFS (or DFS) from any uncolored node, give it color 0, and give every neighbor the opposite color. If you ever find an edge whose endpoints have the same color, it is impossible.
The graph may be disconnected, so start a new BFS from every node that is still uncolored.
Equivalent characterization: a graph is bipartite iff it has no odd-length cycle. The coloring check detects such a cycle automatically.
Walkthrough: n = 3, dislikes [[1,2],[1,3],[2,3]] — a triangle. Color 1 red, 2 and 3 blue, but 2 and 3 dislike each other -> false.
Optimal Approach
Step 1: Build an adjacency list from dislikes.
Step 2: color = [-1] * (n + 1).
Step 3: For each person s with color[s] == -1:
Set color[s] = 0 and BFS.
For each neighbor v of u: if uncolored, set color[v] = 1 - color[u] and enqueue; if color[v] == color[u], return false.
Step 4: Return true.
Time: O(n + e). Space: O(n + e).
Two-coloring by BFS
What Trips People Up in Real Interviews
Only starting BFS from person 1. Disconnected components must each be checked.
Using a directed graph. Dislike is symmetric — add both directions.
Off-by-one: people are numbered from 1 to n, so size arrays n + 1.
Trying greedy assignment in input order without propagation; it can place someone before their constraints are known.
Solution Code
O(n + e) timefrom collections import deque
def possibleBipartition(n, dislikes):
g = [[] for _ in range(n + 1)]
for a, b in dislikes:
g[a].append(b)
g[b].append(a)
color = [-1] * (n + 1)
for s in range(1, n + 1):
if color[s] != -1:
continue
color[s] = 0
q = deque([s])
while q:
u = q.popleft()
for v in g[u]:
if color[v] == -1:
color[v] = 1 - color[u]
q.append(v)
elif color[v] == color[u]:
return False
return TrueFrequently Asked Questions
What is the Possible Bipartition problem?
Possible Bipartition asks whether `n` people can be split into two groups so that no two people who dislike each other end up in the same group. Treat dislikes as edges and the question becomes: is this graph bipartite?
How do you solve Possible Bipartition?
The optimal approach runs in O(n + e) time and O(n + e) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Possible Bipartition?
Possible Bipartition is asked at TikTok, Snowflake. It is a medium difficulty problem.
What are common mistakes on Possible Bipartition?
- Only starting BFS from person 1. Disconnected components must each be checked.
- Using a directed graph. Dislike is symmetric — add both directions.
- Off-by-one: people are numbered from 1 to `n`, so size arrays `n + 1`.
- Trying greedy assignment in input order without propagation; it can place someone before their constraints are known.