← Questions

Pick the root that needs the fewest edge reversals

0
UberCodingLast asked May 2026

A coding interview question asked in Uber's online assessment, last asked May 2026. It tests trees, rerooting and graphs.

You are given a tree with n nodes labeled 1 to n. Its n - 1 edges are directed: each edge (u, v) points from u to v. Ignoring direction, the edges connect all nodes into a tree.

You may pick any node as the root. Once the root is picked, every edge must point away from the root (from the endpoint closer to the root toward the one farther away). To get there you may reverse edges; each reversal costs 1.

Implement:

def min_root_reversals(n: int, edges: list[tuple[int, int]]) -> int

Return the minimum number of reversals over all choices of root.

Constraints

  • 1 <= n <= 10^5
  • len(edges) == n - 1, 1 <= u, v <= n, u != v
  • At most one edge between any pair of nodes; the undirected edges form a tree.

Example 1

n = 4, edges = [(2, 1), (2, 3), (4, 3)]
-> 1

Rooted at 2, edges 2 -> 1 and 2 -> 3 already point away; 4 -> 3 must become 3 -> 4, so 1 reversal. Rooting at 4 also costs 1 (flip 2 -> 3). No root costs 0.

Example 2

n = 4, edges = [(2, 1), (3, 1), (4, 1)]
-> 2

Every edge points into node 1. Rooting at a leaf, say 2, keeps 2 -> 1 and flips the other two.

Aim for better than trying every root with a separate traversal. Be ready to return the cost for every root as well.

Clarifying questions

  • Is n = 1 allowed, with no edges and an answer of 0?
  • Are node labels exactly 1..n, or can they be arbitrary integers?
  • Should the function also report which root achieves the minimum, or all roots that tie?
  • Is the tree deep enough (a chain of 10^5) that recursion would overflow?

What a strong answer covers

  • Roots the tree once at an arbitrary node and counts the edges that point toward that root: the cost for that root, in one traversal.
  • Re-roots: moving the root from u to its neighbour v changes the cost by exactly +1 or -1, depending on whether the edge between them points u -> v or v -> u. A second traversal gives the cost of every root in O(n) total.
  • Stores each undirected edge with a direction flag in the adjacency list so both passes read it in O(1).
  • Uses an iterative DFS or BFS so a long chain does not overflow the stack.
  • Tests a star with every edge pointing in (example 2), a chain pointing one way (answer 0), and a single node.

Follow-ups

  • Return the cost for every root, then the list of roots that achieve the minimum.
  • Give each edge its own reversal cost.
  • The graph is a general directed graph instead of a tree: what is the minimum to make every node reachable from the chosen root?

Where it's asked

  • Uber · Online assessment · Last asked May 202690 min · Two problems on a HackerRank-based platform, camera and screen share on · No AI

What it tests

trees · rerooting · graphs · bfs

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding