← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Uber SWE interview with a graph/tree problem that looks straightforward until you think about scale. The edge reversal re-rooting question is genuinely tricky and I fumbled the implementation details under pressure.

Questions Asked (1)

Q1

You have a directed graph on n nodes (labeled 0 to n-1) where the edges form an undirected tree. For each node i, find the minimum number of edge reversals required so that node i is reachable from node 0. Return an array of length n.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I went straight for recursive DFS and got maybe halfway through before the interviewer asked what happens with large n.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a tree rooted at node 0, where each edge has a cost: 0 if it points away from the root, 1 if it points toward the root. Then compute the sum of costs along the path from the root to each node using a single DFS/BFS, which gives the minimum reversals for each node. This works because reversing an edge only affects the direction of that edge, and the path from root to node is unique in a tree.

Pro tip: Clarify that the graph is a tree (undirected edges with directions) and that the minimum reversals for a node is simply the number of edges on the unique path from root that are directed toward the root. This avoids overcomplicating with dynamic programming or multiple passes.

1. Understand the problem and constraints

Confirm that the graph is a tree (n nodes, n-1 edges) and that we need the minimum reversals for each node to be reachable from node 0. Note that reversing an edge changes its direction, and we want the minimum number of such changes.

2. Define edge costs

Assign a cost of 0 to an edge if it is directed away from the root (i.e., from parent to child in the rooted tree) and a cost of 1 if it is directed toward the root (child to parent). This cost represents the need to reverse that edge.

3. Traverse the tree from root

Perform a DFS or BFS starting from node 0, keeping track of the cumulative cost from the root to the current node. For each child, add the cost of the edge connecting them.

4. Compute and store results

For each node, the cumulative cost from the root is the minimum number of reversals needed. Store these values in an array of length n.

5. Analyze complexity and edge cases

The algorithm runs in O(n) time and O(n) space. Consider edge cases: n=1 (result [0]), and ensure the tree is connected.

Key Points to Mention

  • The graph is a tree, so there is a unique path from the root to any node.
  • Minimum reversals equals the number of edges on the path that are directed toward the root.
  • Use DFS/BFS to compute cumulative costs in a single pass.
  • Time complexity O(n) and space complexity O(n) are optimal.
  • Edge direction relative to the root determines cost (0 if away, 1 if toward).
  • Handle edge cases like n=1 and ensure the tree is connected.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.