PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

Check if two-group seating is possible evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • Medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Check if two-group seating is possible

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given an undirected graph represented as an adjacency list where adj[i] lists the indices of guests who know guest i, determine whether all guests can be seated using at most two groups such that no two guests in the same group know each other. Return true if such a seating is possible, otherwise false. Describe and implement an algorithm that handles disconnected components, and analyze time and space complexity. State assumptions about bidirectionality of edges, presence of self-loops or duplicate entries, and outline edge cases and tests.

Quick Answer: Check if two-group seating is possible evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

You are given an undirected graph as an adjacency list `adj`, where `adj[i]` lists the indices of guests who know guest `i`. You must seat every guest into at most two groups so that no two guests in the same group know each other. Return `true` if such a two-group seating is possible, otherwise `false`. This is equivalent to deciding whether the friendship graph is **bipartite**. Your solution must handle **disconnected components** (run the check from every unvisited node). **Assumptions** (state these before coding): - Edges are bidirectional: if `j` appears in `adj[i]`, the pair `(i, j)` is an undirected acquaintance edge. (The reference treats edges that appear from either endpoint; for a well-formed input each edge is listed on both sides.) - A **self-loop** (`i` in `adj[i]`) means a guest "knows" themselves and they cannot be placed in any group consistently, so the answer is `false`. - **Duplicate entries** in an adjacency list are harmless and treated the same as a single edge. - Guests are indexed `0 .. n-1` where `n = len(adj)`. **Example 1:** `adj = [[1], [0]]` -> `true` (two guests who know each other go in different groups). **Example 2:** `adj = [[1,2],[0,2],[0,1]]` -> `false` (a triangle / odd cycle cannot be 2-colored). **Example 3:** `adj = []` -> `true` (no guests, vacuously seatable).

Constraints

  • 0 <= n <= 10^5 where n = len(adj)
  • 0 <= adj[i][k] < n (indices are valid guests)
  • Total number of adjacency entries (edges counted from both sides) up to ~2*10^5
  • Input may contain disconnected components, duplicate entries, or self-loops
  • Return a boolean

Examples

Input: ([[1], [0]],)

Expected Output: True

Explanation: Two guests who know each other go into different groups — bipartite.

Input: ([[1, 2], [0, 2], [0, 1]],)

Expected Output: False

Explanation: Triangle (odd cycle): three mutual acquaintances cannot be split into two groups.

Input: ([],)

Expected Output: True

Explanation: No guests — vacuously seatable.

Input: ([[]],)

Expected Output: True

Explanation: One isolated guest — trivially seatable in one group.

Input: ([[0]],)

Expected Output: False

Explanation: Self-loop: a guest 'knows' themselves and cannot be consistently seated.

Input: ([[1], [0], [3], [2]],)

Expected Output: True

Explanation: Two separate edges (disconnected components), each 2-colorable independently.

Input: ([[1, 3], [0, 2], [1, 3], [0, 2]],)

Expected Output: True

Explanation: A 4-cycle (even cycle) is bipartite: alternate groups around the cycle.

Input: ([[1, 2, 3], [0], [0], [0, 4], [3]],)

Expected Output: True

Explanation: A tree-like structure with a branch is always bipartite.

Input: ([[1, 1], [0, 0]],)

Expected Output: True

Explanation: Duplicate edges are harmless; still a single bipartite edge between two guests.

Hints

  1. Two groups + 'same group must not know each other' is exactly a graph 2-coloring problem. The answer is 'is the graph bipartite?'
  2. BFS or DFS from each unvisited node, assigning the opposite color to every neighbor. A conflict (a neighbor already has the same color) means it is impossible.
  3. Iterate the outer loop over ALL nodes so disconnected components are each colored. A self-loop is an instant failure since the node would need a different color from itself.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

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

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL 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.

Related Coding Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Solve common string/DP/stack problems - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)
  • Implement stack variants and path-sum check - TikTok (medium)