Split Players into Two Teams So No Rivals Share a Team

Last updated: November 29, 2026

Quick Overview

Given n players and a list of rival pairs, assign each player to team 0 or team 1 so that no rival pair is on the same team, or return None if that is impossible.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Given n players and a list of rival pairs, assign each player to team 0 or team 1 so that no rival pair is on the same team, or return None if that is impossible.

Framed around matchmaking for a Google Play game or Stadia-style lobby, this is a bipartite check. The interviewer wants to see BFS or DFS two-coloring, iteration over every connected component, and a crisp explanation that an odd cycle is exactly what makes it impossible.

What the Interviewer Expects
  • Models players as nodes and rivalries as undirected edges
  • Two-colors with BFS, alternating colors across edges
  • Starts a new BFS for every uncolored component
  • Detects conflicts and returns None
  • Explains the odd-cycle characterization
Key Topics to Cover
Graphs
Bipartite
BFS
Graph Coloring
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Rivalries arrive one at a time; detect the first one that makes a split impossible.
  • Teams must also be equal in size; is that still easy?
  • Allow three teams; what happens to the complexity?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: n players labeled 0..n-1, list of undirected rival edges.
  • Output: list of 0/1 team ids satisfying all edges, or None.
  • Isolated players can go anywhere....
High-Level Design

Build an adjacency list and a team array initialized to -1. For each player still uncolored, color it 0 and BFS: for each neighbor, if uncolored, assign the opposite color and enqueue; if colored th...


Submit Your Answer
Markdown supported