Check Bipartite Graph
Updated
Problem statement
You are given a square binary matrix edges describing an undirected graph with vertices numbered from 0 through n - 1.
Vertices i and j are connected when edges[i][j] == 1 or edges[j][i] == 1. This convention allows an edge to be recorded in either half of the matrix.
Return true if the vertices can be divided into two groups such that every edge joins vertices from different groups. Otherwise, return false.
Function
isGraphBipartite(edges: int[][]) → booleanExamples
Example 1
edges = [[0,1,0,0],[0,0,0,1],[0,0,0,0],[0,0,0,0]]return = trueThe only edges are 0-1 and 1-3. One valid partition is {0, 3} and {1, 2}.
Example 2
edges = [[0,1,1],[0,0,1],[0,0,0]]return = falseVertices 0, 1, and 2 form an odd cycle, so two groups are impossible.
Example 3
edges = [[0,1,0,1,0],[1,0,1,0,0],[0,1,0,1,0],[1,0,1,0,0],[0,0,0,0,0]]return = trueThe four connected vertices form an even cycle, and vertex 4 is isolated. Every component is bipartite.
Constraints
2 <= edges.length <= 300edges[i].length == edges.lengthedges[i][j]is either0or1.edges[i][i] == 0- An undirected edge is present when either
edges[i][j]oredges[j][i]is1.
More InMobi Online Assessment questions
- Validate a Bracket SequenceOA · Seen Oct 2026
- Count Substrings Matching with One Adjacent SwapOA · Seen Oct 2026
- Maximum Subarray with Start and End WindowsOA · Seen Oct 2026
- Minimum Service Cold StartsOA · Seen Oct 2026
- Range Maximum Subarray Sum QueriesOA · Seen Oct 2026
- Connected Components with Divisor ThresholdOA · Seen Aug 2026
- Maximize Impression ValueOA · Seen Aug 2026
- Supply Path OptimizationOA · Seen Aug 2026
- Delivery ServiceOA · Seen Jun 2026
- Shared InterestOA · Seen Jun 2026
Similar questions at other companies
- Minimize CommuteDatabricks · OA · PHONE SCREENDatabricks interview questions
- Shortest Atom Action PathOpenAI · OAOpenAI interview questions
- Minimum Cleaning Robot RunsHSBC · OAHSBC interview questions
- Count Islands with DFS and BFSVisa · OAVisa interview questions
- Number of IslandsGoldman Sachs · OA · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Count One Groups by SizeAgoda · OAAgoda interview questions
- Ascend Vertical Wall (For off-campus 2025 batch hiring :)Uber · OAUber interview questions
- Grid ExplorationIMC Trading · OAIMC Trading interview questions
- Maximum Damage PatchGecko Robotics · OA · PHONE SCREEN
- Nearest Different Maze Exit for a SnakeIntuit · OAIntuit interview questions