Data Structures & AlgorithmsVery hardICPC regional · GATE CS
Problem: In the undirected graph on vertices 0–4 with edges 0-1, 0-2, 1-2, 1-3, 2-3, 3-4, how many Hamiltonian paths exist (paths that visit every vertex exactly once; a path and its reverse count as different paths)?
- A. 4
- B. 2
- C. 120
- D. 8
Correct answer: D. 8
Backtracking over all orders finds 8 directed Hamiltonian paths (4 undirected ones, each counted in both directions; every path must end or start at vertex 4, whose only neighbour is 3). Counting a path and its reverse once gives 4, counting only paths that start at 0 gives 2, and 5! = 120 ignores edges.
More Coding Interview Problems questions
- Problem: On a rooted tree (root 1) with children: 1: [2, 3], 2: [4, 5, 6], 3: [7], 5: [8, 9]. How many nodes have exactly 2 descendants…
- Problem: A binary tree is encoded in preorder with '#' for a missing child: "1,2,#,#,3,4,#,#,5,#,#". How many leaves (nodes with two…
- Problem: Count the palindromic substrings of length exactly 4 in the string "aabbaabbaa".
- Problem: How many distinct two-letter substrings does "mississippi" contain?
- Problem: The smallest period of a string s is the least p ≥ 1 such that s[i] = s[i+p] for all valid i. What is the smallest period of…
- Problem: What is the length of the longest proper border (a proper prefix that is also a suffix) of "abacabacaba"? A proper border is…