Divide Colored Balls with Sticks (Bipartite Graph)

Quick Overview

Decide whether balls joined by sticks can be split into two boxes so that no stick connects two balls in the same box. The problem tests modeling a constraint as an undirected graph, checking whether that graph is bipartite, and covering every disconnected component.

Divide Colored Balls with Sticks (Bipartite Graph)

Company: Miro

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given `n` colored balls labeled from `1` to `n` and a list `edges`, where each `edges[i] = [a, b]` is a stick connecting balls `a` and `b`. Two balls connected by a stick must be placed in different boxes, because they should not be the same color. Determine whether it is possible to divide all the balls into two boxes so that no stick connects two balls in the same box. Return `True` if such a division is possible, and `False` otherwise. In other words, given an undirected graph, decide whether the graph is bipartite. ### Function Signature ```python def is_bipartite(n: int, edges: list[list[int]]) -> bool: ``` ### Rules - Balls are numbered `1` to `n`. Each pair `[a, b]` in `edges` is an undirected stick between balls `a` and `b`. - The sticks may form several separate groups, and some balls may have no sticks at all. Every ball still has to go into one of the two boxes. - A box may be left empty, so an input with no sticks returns `True`. ### Constraints - `1 <= n <= 2000` - `0 <= len(edges) <= n * (n - 1) / 2` - `len(edges[i]) == 2` - `1 <= edges[i][0], edges[i][1] <= n` - `edges[i][0] != edges[i][1]` - There are no repeated edges in the input. ### Examples ```text Input: n = 4, edges = [[1,2],[2,3],[3,4],[4,1]] Output: True ``` The sticks form the 4-cycle 1-2-3-4-1. Put balls 1 and 3 in one box and balls 2 and 4 in the other, and every stick joins balls in different boxes. ```text Input: n = 3, edges = [[1,2],[2,3],[1,3]] Output: False ``` The sticks form a triangle. Whichever box ball 1 goes into, balls 2 and 3 both have to go into the other box, and they are joined by a stick. ```text Input: n = 5, edges = [[1,2],[3,4],[4,5],[5,3]] Output: False ``` Balls 1 and 2 split into the two boxes without trouble, but balls 3, 4 and 5 form a separate triangle that cannot be split. A single group with no valid split makes the whole answer `False`.

Overview: Decide whether balls joined by sticks can be split into two boxes so that no stick connects two balls in the same box. The problem tests modeling a constraint as an undirected graph, checking whether that graph is bipartite, and covering every disconnected component.

Miro logo
Miro
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given n colored balls labeled from 1 to n and a list edges, where each edges[i] = [a, b] is a stick connecting balls a and b. Two balls connected by a stick must be placed in different boxes, because they should not be the same color.

Determine whether it is possible to divide all the balls into two boxes so that no stick connects two balls in the same box. Return True if such a division is possible, and False otherwise.

In other words, given an undirected graph, decide whether the graph is bipartite.

Function Signature

def is_bipartite(n: int, edges: list[list[int]]) -> bool:

Rules

  • Balls are numbered 1 to n . Each pair [a, b] in edges is an undirected stick between balls a and b .
  • The sticks may form several separate groups, and some balls may have no sticks at all. Every ball still has to go into one of the two boxes.
  • A box may be left empty, so an input with no sticks returns True .

Constraints

  • 1 <= n <= 2000
  • 0 <= len(edges) <= n * (n - 1) / 2
  • len(edges[i]) == 2
  • 1 <= edges[i][0], edges[i][1] <= n
  • edges[i][0] != edges[i][1]
  • There are no repeated edges in the input.

Examples

Input: n = 4, edges = [[1,2],[2,3],[3,4],[4,1]]
Output: True

The sticks form the 4-cycle 1-2-3-4-1. Put balls 1 and 3 in one box and balls 2 and 4 in the other, and every stick joins balls in different boxes.

Input: n = 3, edges = [[1,2],[2,3],[1,3]]
Output: False

The sticks form a triangle. Whichever box ball 1 goes into, balls 2 and 3 both have to go into the other box, and they are joined by a stick.

Input: n = 5, edges = [[1,2],[3,4],[4,5],[5,3]]
Output: False

Balls 1 and 2 split into the two boxes without trouble, but balls 3, 4 and 5 form a separate triangle that cannot be split. A single group with no valid split makes the whole answer False.