Data Structures & AlgorithmsModerateAccenture · Cognizant coding round pattern

Problem: Count how many of these four undirected graphs are bipartite (2-colourable so that every edge joins different colours): G1: edges 0-1, 1-2, 2-3, 3-0 G2: edges 0-1, 1-2, 2-0 G3: edges 0-1, 0-2, 1-3, 2-4, 3-5, 4-5 G4: edges 0-1, 1-2, 2-3, 3-4, 4-0, 0-2 How many are bipartite?

  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 4

Correct answer: B. 2

A graph is bipartite exactly when BFS two-colouring never meets an edge with equal colours, which means no odd cycle. G1 (4-cycle) and G3 (6-cycle) are bipartite; G2 contains the triangle 0-1-2 and G4 contains the odd 5-cycle 0-1-2-3-4, so both fail.

More Coding Interview Problems questions

Exams that ask this topic