Data Structures & AlgorithmsModerateTCS NQT · Product Company SDE
Problem: A DAG has 4 vertices with edges 0→2 and 1→2 and 2→3 (vertices 0, 1 have no incoming arcs). How many distinct topological orders does it have?
- A. 3
- B. 1
- C. 2
- D. 4
Correct answer: C. 2
Enumerate all orders and keep those that respect every arc. Counting orders of the vertices that are source-free without checking the arc 2→3 overcounts.
More Coding Interview Problems questions
- 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…
- 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…