Data Structures & AlgorithmsVery hardAmazon · Microsoft coding round

Problem: A bipartite graph check is commonly done by attempting a 2-coloring via BFS/DFS. What does finding an edge between two same-colored nodes indicate?

  1. A. The graph is not bipartite (an odd-length cycle exists)
  2. B. The graph has a negative cycle
  3. C. The graph is disconnected
  4. D. The graph must be a tree

Correct answer: A. The graph is not bipartite (an odd-length cycle exists)

If BFS/DFS ever tries to color a neighbour the same as its parent along the 2-coloring process, that edge forces two nodes at the same 'side' to be adjacent, which can only happen if some cycle in the graph has odd length -- exactly the condition that rules out bipartiteness.

More Coding Interview Problems questions

Exams that ask this topic