Data Structures & AlgorithmsModerateGATE CS · ICPC regional
Problem: An undirected graph has 8 vertices and 11 edges with exactly 2 connected components. What is the minimum number of edges that must be removed so the graph becomes a forest (no cycles)?
- A. 6
- B. 4
- C. 5
- D. 7
Correct answer: C. 5
A forest with c components on n vertices has n − c edges, so the number of edges to remove is m − (n − c). Removing m − n edges ignores the component count and removes one too few.
More Coding Interview Problems questions
- Problem: For the path graph P5 on vertices 1–5 (edges 1-2, 2-3, 3-4, 4-5), what is the sum of the distances over all unordered pairs of…
- Problem: The string "abcdabcd" is examined for windows of length 4 with all four characters distinct. How many such windows are there?
- Problem: What is the minimum number of characters to delete from "programming" so that all remaining characters are distinct (keep one…
- Problem: Compute the longest palindromic prefix of "abacabacaba" (the longest prefix that reads the same backwards). What is its length?
- Problem: What is the length of the longest common substring (contiguous) of "abcdxyz" and "xyzabcd"?
- Problem: From the array [3, 2, 7, 10], choose a subset with no two adjacent indices chosen, maximising the sum. What is the maximum sum?