Data Structures & AlgorithmsHardAmazon/Microsoft SDE pattern
Undirected graph edges: A–D, D–C, B–C, C–A. Using a BFS 2-colouring, is this graph bipartite?
- A. Cannot be determined without edge weights
- B. Yes, it is bipartite
- C. No, it contains an odd-length cycle
- D. Yes, but only if the graph is also a tree
Correct answer: C. No, it contains an odd-length cycle
BFS 2-colouring assigns alternating colours level by level from any start vertex; the graph is bipartite exactly when no edge ever connects two same-coloured vertices, which happens exactly when the graph has no odd-length cycle. Here the colouring fails, so the answer is "No, it contains an odd-length cycle".
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Keys 57, 11, 34, 33, 80, 9, 72, 24, 56, 66 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain)…
- Undirected graph edges: B–C, C–A, E–C, E–D, E–B. Using a BFS 2-colouring, is this graph bipartite?
- Build a binary min-heap in place from the array [4, 37, 81, 88, 64, 1, 84, 86] using bottom-up heapify (sift-down from the last internal…
- An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [D, F]; B: [C, F]; C: [B, E]; D: [A, E]; E: [C, D]…
- Keys 93, 67, 60, 15, 3, 58, 52, 13, 92 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain) into…
- Build a binary min-heap in place from the array [35, 63, 89, 24, 71, 8, 16, 88, 2, 79] using bottom-up heapify (sift-down from the last…