Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional
Problem: A directed graph has vertices 0–6 and arcs: 0→1, 1→2, 2→0, 2→3, 3→4, 4→5, 5→3, 5→6, 6→6. A vertex lies on a cycle if it can reach itself by following at least one arc. How many vertices lie on at least one directed cycle (a self-loop counts as a cycle)?
- A. 7
- B. 6
- C. 3
- D. 5
Correct answer: A. 7
Vertices 0, 1, 2 lie on 0→1→2→0, vertices 3, 4, 5 on 3→4→5→3, and 6 on its self-loop, so all 7 qualify; 2→3 and 5→6 only join cycles. Ignoring the self-loop gives 6, counting cycles (SCCs) instead of vertices gives 3, and marking only the endpoints of DFS back edges gives 5.
More Coding Interview Problems questions
- Problem: Count how many of these four undirected graphs are bipartite (2-colourable so that every edge joins different colours): G1: edges…
- 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…