Reported April 2026
Uberdepth first search

Minimum Edge Reversals to Root a Tree

Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Uber OA. Under 2s to a working solution.
Founder's read

You've got an Uber OA in April and they're asking you to find the minimum edge reversals needed to root a tree. The catch: you pick the root, and every edge must point away from it. This is a graph problem that looks harder than it is. Most candidates freeze because they think they need to try every node as root and run a full DFS each time. That's the trap. StealthCoder will spot the pattern if you blank on the reroot technique, but understanding the trick now means you won't need it.

The problem

You are given a directed version of a tree with n nodes labeled from 0 to n - 1. Each undirected edge of the tree appears once in the input as a directed edge u -> v. You may choose any node as the root. After choosing the root, every edge should point away from that root. You may reverse edges, and each reversed edge costs 1. Return the minimum number of edge reversals needed over all possible root choices. Function Description Complete the function minEdgeReversalsForRoot in the editor below. minEdgeReversalsForRoot has the following parameters: Returns The source thread did not provide explicit numeric bounds.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The key insight is dynamic programming with rerooting. First, calculate the cost to make node 0 the root by doing a single DFS: count reversals needed. Then use a second DFS to reroot efficiently. When you move the root from a parent to a child, you flip the direction of one edge. If that edge originally pointed child to parent, you add 1. If it pointed parent to child, you subtract 1. This is O(n) total, not O(n squared). Most candidates try brute force and run out of time. The reroot trick is the actual pattern. StealthCoder can generate the DP solution fast if you're stuck mid-OA, but the real win is knowing this is a rerooting problem before you start coding.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Minimum Edge Reversals to Root a Tree cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Uber's OA.

Uber reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Edge Reversals to Root a Tree FAQ

Is this really asking me to try every node as root?+

Technically yes, but not independently. Calculate cost for one root, then use dynamic programming to reroot in linear time. Brute force is O(n squared) and will timeout. Rerooting is the actual pattern Uber expects here.

What's the trick to reroot efficiently?+

When you move the root from parent to child, one edge flips direction. Check the original direction of that edge: if it pointed child to parent, add 1 to cost. If it pointed parent to child, subtract 1. This updates the cost in O(1) per edge.

How do I know which direction edges originally pointed?+

Store the original edges as a set of tuples or an adjacency structure that distinguishes direction. When you traverse from parent to child during the second DFS, check if that direction exists in the original input. If not, the edge was reversed in the first tree.

Should I be worried about the tree structure being disconnected?+

The problem states it's a tree with n nodes, so it's connected. No cycles, no isolated components. A single DFS from any starting node reaches everything.

What if I blank on the reroot approach during the OA?+

Start with the brute force: pick node 0, run DFS to count reversals, store the result. Then loop through other nodes and repeat. It's slow, but correct. You can optimize later. A working slow solution beats no solution, and StealthCoder can suggest the reroot fix if time allows.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Uber.

OA at Uber?
Invisible during screen share
Get it