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?
- A. 3
- B. 2
- C. 1
- 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
- Problem: Find the length of the longest palindromic substring of the string "abacdfgdcabazyx".
- Problem: Using insert, delete and replace, each costing 1, what is the minimum number of operations to convert "dragon" into "lantern"?
- Problem: How many palindromic substrings does "aabbaa" contain? Two substrings count as different if they occupy different index ranges…
- Problem: Compute the KMP prefix function (the longest proper prefix that is also a suffix, for each prefix) of the pattern "abacabab"…
- Problem: Count the occurrences of the pattern "aaa" in the text "aaabaaaab", where occurrences may overlap (each starting index that…
- Problem: Round-robin scheduling with time quantum 3 is applied to processes that all arrive at time 0: P1 needs 7 units, P2 needs 4, P3…