Data Structures & AlgorithmsVery hardProduct company OA
A course plan has prerequisites (a → b means a must come before b): 1→3, 2→3, 3→5, 4→5, 5→6, 2→4, 6→7. Any number of courses with all prerequisites done can be taken in one semester. What is the minimum number of semesters?
- A. 4
- B. 7
- C. 3
- D. 5
Correct answer: D. 5
The answer is the number of vertices on the longest path, found level by level with Kahn's algorithm: semester 1: {1, 2}; 2: {3, 4}; 3: {5}; 4: {6}; 5: {7}. Longest chain 1 → 3 → 5 → 6 → 7 (or 2 → 4 → 5 → 6 → 7) has 5 courses. '7' takes one course per semester.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Flights (directed, cost): 0→1 100, 1→2 100, 2→3 100, 0→2 500, 1→3 600, 0→3 900. What is the cheapest cost from 0 to 3 using AT MOST 1…
- What is the diameter (largest distance between any two nodes) of the weighted tree with edges 1–2 (3), 1–3 (4), 2–4 (2), 2–5 (6), 3–6 (5)…
- Running median with two heaps (a max-heap for the lower half, a min-heap for the upper half, sizes differing by at most 1, the lower half…
- Each array is a binary tree in level order (0-indexed, children at 2i + 1 and 2i + 2, all positions present). Which one is a valid BST?
- Binary trees with node labels A, B, C have preorder A B C and postorder C B A. How many different binary trees match BOTH traversals?
- An alien language uses only the letters a, b, c, d. The words bca, bcd, ad, ac, dc are listed in sorted order. Building an edge from each…