Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: Starting from the tree with root 1 and children 1: [2, 3], 2: [4], 3: [5, 6], 5: [7], a DFS visits children in increasing order. Which node is last to be finished (post-order last visited)?
- A. 2
- B. 0
- C. 1
- D. 3
Correct answer: C. 1
In post-order the root is always last. The question asks about the last node finished under the given order, which is the root in any post-order traversal, so the trap is answering with the last leaf instead.
More Coding Interview Problems questions
- Problem: For each index of a = [4, 5, 2, 10, 8], find the index of the nearest element to its left that is strictly smaller (or -1). Run…
- Problem: A directed flow network has source s = 0 and sink t = 3 with arcs and capacities: 0→1 (3), 0→2 (2), 1→2 (1), 1→3 (2), 2→3 (3)…
- Problem: A bottleneck path from vertex 0 to vertex 4 minimises the largest edge weight used. Using the undirected edges 0-1 (7), 1-2 (3)…
- 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…
- 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…