Is Graph Bipartite?
Problem
You are given an undirected graph as an adjacency list: graph[u] lists the neighbors of node u, for u in 0..n-1. Decide whether the nodes can be split into two groups such that every edge connects a node in one group to a node in the other. The graph may be disconnected and may have isolated nodes; it has no self-loops or repeated edges.
- There are at most a few hundred nodes.
graph[u]does not containu, and ifvis ingraph[u]thenuis ingraph[v].
Examples
graph = [[1,2],[0,3],[0,3],[1,2]]→true. Groups{0, 3}and{1, 2}: a 4-cycle.graph = [[1,2,4],[0,2],[0,1,4],[4],[0,2,3]]→false. Nodes 0, 1, 2 form a triangle, and three mutually adjacent nodes cannot fit in two groups.graph = [[], [2], [1]]→true. Node 0 is isolated and can go in either group.
Hints
- Pick a node and put it in group A. Where must all of its neighbors go? And their neighbors?
- Following that rule, when would you be forced to put a node in both groups?
- Two-color the graph with BFS/DFS: color each newly reached node the opposite of its discoverer; a neighbor that already has the same color means the graph is not bipartite. Restart from every uncolored node.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.