Skip to interview prep
Apexed

97. Is the Graph Bipartite?

MediumGraphs

An undirected graph is given as an adjacency list: graph[i] lists the neighbours of vertex i. Return true if the vertices can be split into two groups so that every edge joins vertices from different groups.

The graph may be disconnected and has no self-loops.

Example 1

Input: graph = [[1,3],[0,2],[1,3],[0,2]]
Output: true

Example 2

Input: graph = [[1,2,3],[0,2],[0,1,3],[0,2]]
Output: false

Constraints

  • 0 ≤ n ≤ 100

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Is the Graph Bipartite? — coding interview practice — Apexed