Find minimum reversals to orient edges away from root
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: This question evaluates understanding of directed graphs and rooted trees, the ability to reason about edge orientations and minimal reversals, and competency in traversal techniques and complexity analysis.
Constraints
- 1 <= n <= 2 * 10^5
- edges.length == n - 1
- The undirected version of edges forms a tree (connected, acyclic)
- 0 <= u, v, r < n
- Each edge is a directed pair (u, v); input orientation may already be correct or reversed
Examples
Input: (5, [(1, 0), (0, 2), (3, 2), (3, 4)], 0)
Expected Output: 2
Explanation: The worked example. Edges (1,0) and (3,2) point toward root 0 along their branches, so both must be reversed.
Input: (1, [], 0)
Expected Output: 0
Explanation: A single node with no edges is already a valid rooted tree; nothing to reverse.
Hints
- Rooting the tree at r forces a unique target direction for every edge: parent -> child. There is no choice to optimize across edges, so the answer is just the count of edges whose current direction disagrees.
- Build an undirected adjacency list so you can walk the tree in any direction, but tag each entry with a cost: for original edge (u, v), add (v, 0) to adj[u] (forward = free) and (u, 1) to adj[v] (backward = one reversal).
- Traverse outward from r with BFS or an explicit stack and sum the cost tag of each edge crossed in the parent -> child direction. Use an iterative traversal — a recursive DFS can overflow the stack on a 2e5-deep path.