Data Structures & AlgorithmsHardProduct Company SDE · graph round
Problem: Junctions 1..6 form a precedence graph with edges 4 to 1, 3 to 1, 2 to 5 (an edge a to b means a must come before b). Kahn's algorithm, using a min-heap of zero in-degree nodes, produces the lexicographically smallest topological order over nodes 1..5. Which order does it produce?
- A. 2 3 4 5 1
- B. 4 3 2 1 5
- C. 2 3 4 1 5
- D. 2 4 3 1 5
Correct answer: C. 2 3 4 1 5
Zero in-degree nodes start as {2,3,4}; the heap pops 2 (releasing 5), then 3, then 4 (releasing 1), and finally the smallest of {1,5} is 1 before 5. The order 2 3 4 5 1 is valid but not lexicographically smallest.
More Coding Interview Problems questions
- Problem: Kruskal's algorithm with union-find is run on 5 nodes (0..4) with weighted edges (4,0,1), (1,1,2), (3,0,2), (2,2,3), (7,3,4)…
- Problem: Count the subsets (including the empty subset) of {1, 2, 3, 4, 5, 6} whose sum is divisible by 3. python a = [1, 2, 3, 4, 5, 6]…
- Problem: How many integers x with 1 <= x <= 100 have no two adjacent equal decimal digits? Use the rule that a one-digit number always…
- Problem: What is the sum of all divisors of 360 = 2^3 3^2 5?
- Problem: Find the smallest positive x satisfying x ≡ 2 (mod 3), x ≡ 3 (mod 5) and x ≡ 2 (mod 7).
- Problem: Which pair (x, y) satisfies 240x + 46y = gcd(240, 46)?