Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Split people into two teams

    MediumAbout 25 minFull AccessLearn it: Graphs with BFS and DFS in C#: Adjacency Lists, Grid Shortest Paths, Islands and Bipartite Checks

    The task

    A quiz night puts everyone into two teams, and some pairs of people are rivals who must play against each other.

    Complete CanSplitInTwo(int n, int[][] rivals). People are numbered 0 to n - 1, and each pair [a, b] says that a and b must be in different teams. Return true if everyone can be put in one of two teams so that every pair is split, otherwise false. A pair can be listed twice, and a pair [a, a] can never be split. A team may be empty.

    Signature

    public static bool CanSplitInTwo(int n, int[][] rivals)

    Examples

    • 4, [[0, 1], [1, 2], [2, 3], [3, 0]] → true. 0 and 2 form one team, 1 and 3 the other.
    • 3, [[0, 1], [1, 2], [2, 0]] → false. In a ring of three, two rivals always end up together.

    Limits: Up to 200,000 people and 200,000 pairs.

    The tests check: one person, no pairs, a pair listed twice, a pair [a, a], rings of even and odd length, a problem in a second, separate group, groups joined by a later pair, and 200,000 people in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // True when people 0..n-1 can form two teams so that every rival pair is split.
          public static bool CanSplitInTwo(int n, int[][] rivals)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.