Question presented to candidate:
"You are given an undirected graph as an adjacency list — graph[u] is the list of nodes joined to u. Return whether the graph is bipartite: can you split the nodes into two groups so that every edge goes between the groups? The graph may be disconnected. Please write it in Python."
What a strong answer should cover:
- The reformulation: bipartite ⇔ the graph can be 2-coloured ⇔ it has no odd cycle.
- BFS (or DFS) colouring: give each uncoloured neighbour the opposite colour; a neighbour with the same colour means
False. - An outer loop over every node so each connected component gets its own traversal.
- O(V + E) time and O(V) extra space, plus the free result: the colour array is the two sides.
- Python specifics:
collections.dequewithpopleft()for the queue, building adjacency lists with a list comprehension (never[[]] * n), and recursion depth limits if you write the DFS recursively.
Clarifying questions expected:
- Can the graph be disconnected or contain self-loops?
- Do you need only the yes/no answer, or the two groups too?
Code / implementation expected: Yes — an is_bipartite(graph) function in Python with a colour list and BFS, tested on a bipartite graph, a triangle and a disconnected graph.