PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Apple

Determine Whether an Undirected Graph Is Two-Colorable

Last updated: Jul 22, 2026

Quick Overview

Determine whether every component of an undirected graph can be colored with two colors so adjacent nodes differ. The problem tests linear-time graph traversal, disconnected and isolated nodes, conflict detection, parallel edges, self-loops, and correctness on large sparse inputs.

  • medium
  • Apple
  • Coding & Algorithms
  • Software Engineer

Determine Whether an Undirected Graph Is Two-Colorable

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Determine Whether an Undirected Graph Is Two-Colorable Given an undirected graph with nodes labeled `0` through `n - 1`, determine whether every node can be assigned one of two colors so that the endpoints of every edge have different colors. ```python def is_two_colorable(n: int, edges: list[list[int]]) -> bool: ... ``` The graph can be disconnected and can contain isolated nodes. Using only one of the two colors in an isolated component is allowed. Parallel edges may appear and do not change the answer; a self-loop makes the answer `false`. ## Constraints - `0 <= n <= 200_000` - `0 <= len(edges) <= 300_000` - Every edge is a pair `[u, v]` with `0 <= u, v < n`. - Target complexity: `O(n + len(edges))` time and `O(n + len(edges))` space. ## Examples ```text Input: n = 4, edges = [[0, 1], [1, 2], [2, 3], [3, 0]] Output: true ``` ```text Input: n = 3, edges = [[0, 1], [1, 2], [2, 0]] Output: false ``` ```text Input: n = 5, edges = [[0, 1], [3, 4]] Output: true ```

Quick Answer: Determine whether every component of an undirected graph can be colored with two colors so adjacent nodes differ. The problem tests linear-time graph traversal, disconnected and isolated nodes, conflict detection, parallel edges, self-loops, and correctness on large sparse inputs.

Related Interview Questions

  • Solve Subset Sum And Return All Matching Subsets - Apple (medium)
  • Vertical Order Traversal of a Binary Tree - Apple (medium)
  • Convert a Roman Numeral to an Integer - Apple (medium)
  • Minimal Unique Word Abbreviations - Apple (medium)
|Home/Coding & Algorithms/Apple

Determine Whether an Undirected Graph Is Two-Colorable

Apple logo
Apple
Jul 13, 2026, 12:00 AM
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Determine Whether an Undirected Graph Is Two-Colorable

Given an undirected graph with nodes labeled 0 through n - 1, determine whether every node can be assigned one of two colors so that the endpoints of every edge have different colors.

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

The graph can be disconnected and can contain isolated nodes. Using only one of the two colors in an isolated component is allowed. Parallel edges may appear and do not change the answer; a self-loop makes the answer false.

Constraints

  • 0 <= n <= 200_000
  • 0 <= len(edges) <= 300_000
  • Every edge is a pair [u, v] with 0 <= u, v < n .
  • Target complexity: O(n + len(edges)) time and O(n + len(edges)) space.

Examples

Input: n = 4, edges = [[0, 1], [1, 2], [2, 3], [3, 0]]
Output: true
Input: n = 3, edges = [[0, 1], [1, 2], [2, 0]]
Output: false
Input: n = 5, edges = [[0, 1], [3, 4]]
Output: true

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Apple•More Software Engineer•Apple Software Engineer•Apple Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.