Decide Whether an Undirected Graph Can Be Two-Colored

Quick Overview

Decide Whether an Undirected Graph Can Be Two-Colored. graph is an undirected adjacency list that may be disconnected.

Decide Whether an Undirected Graph Can Be Two-Colored

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem graph is an undirected adjacency list that may be disconnected. Decide whether its vertices can be split into two sets so that every edge joins different sets. There are no self-loops or repeated neighbors. ```cpp class Solution { public: bool isBipartite(vector<vector<int>>& graph); }; ``` ### Examples **Example 1:** ```text graph = [[1],[0],[]] Output: true Explanation: Put vertices0 and2 on one side and vertex1 on the other. ``` **Example 2:** ```text graph = [[1,2],[0,2],[0,1]] Output: false Explanation: A triangle cannot be colored using two colors with different ends on every edge. ``` ### Constraints - Vertex count V lies in [1,100], with labels 0 through V-1. - Each neighbor list contains fewer than V distinct valid labels and excludes its own vertex. - Every adjacency appears in both directions. ### Contract considerations Preserve symmetric undirected adjacency and allow disconnected graphs. ```hint Read the boundary condition What color must a neighbor of a colored vertex have? ``` ### Boundary reminders Preserve symmetric undirected adjacency and allow disconnected graphs.

Overview: Decide Whether an Undirected Graph Can Be Two-Colored. graph is an undirected adjacency list that may be disconnected.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

graph is an undirected adjacency list that may be disconnected. Decide whether its vertices can be split into two sets so that every edge joins different sets. There are no self-loops or repeated neighbors.

class Solution {
public:
    bool isBipartite(vector<vector<int>>& graph);
};

Examples

Example 1:

graph = [[1],[0],[]]
Output: true
Explanation: Put vertices0 and2 on one side and vertex1 on the other.

Example 2:

graph = [[1,2],[0,2],[0,1]]
Output: false
Explanation: A triangle cannot be colored using two colors with different ends on every edge.

Constraints

  • Vertex count V lies in [1,100], with labels 0 through V-1.
  • Each neighbor list contains fewer than V distinct valid labels and excludes its own vertex.
  • Every adjacency appears in both directions.

Contract considerations

Preserve symmetric undirected adjacency and allow disconnected graphs.

Boundary reminders

Preserve symmetric undirected adjacency and allow disconnected graphs.