Is Graph Bipartite?
Updated
Problem statement
You are given an adjacency-list representation of an undirected graph. The vertices are numbered from 0 through graph.length - 1, and graph[u] lists every neighbor of vertex u.
Return true when the vertices can be divided into two groups such that every edge connects vertices from different groups. Otherwise, return false.
The graph may be disconnected.
Function
isBipartite(graph: int[][]) → booleanExamples
Example 1
graph = [[1,2,3],[0,2],[0,1,3],[0,2]]return = falseVertices 0, 1, and 2 form an odd cycle.
Example 2
graph = [[1,3],[0,2],[1,3],[0,2]]return = trueOne valid partition is {0, 2} and {1, 3}.
Example 3
graph = [[],[]]return = trueTwo isolated vertices have no conflicting edge.
Constraints
1 <= graph.length <= 500- Every neighbor is in the range from
0throughgraph.length - 1. - No vertex lists itself, and a neighbor appears at most once in one adjacency list.
- The graph is undirected: if
vappears ingraph[u], thenuappears ingraph[v].
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Course ScheduleOracle · ONSITE INTERVIEWOracle interview questions
- Course SchedulePlaud · ONSITE INTERVIEW
- Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
- Evaluate DivisionPhonePe · ONSITE INTERVIEWPhonePe interview questions
- Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Mouse and Cheese ReachabilityMeta · ONSITE INTERVIEWMeta interview questions
- Number of IslandsTripjack · ONSITE INTERVIEWTripjack interview questions
- Number of IslandsTekion · ONSITE INTERVIEWTekion interview questions