← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Uber SWE interview with a graph problem that looks deceptively clean on the surface. One question, but it had enough depth to keep me busy for a while.

Questions Asked (1)

Q1

Given a directed graph, find the root node such that the total number of edge reversals needed to make every edge point away from that root is minimized. The graph is 1-indexed and you should return the minimum reversal count across all possible roots.

Algorithms & Data Structures
Author's notes

My first instinct was to just BFS from every node and count reversals each time, which technically works but blows up on large graphs.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as finding a root that minimizes the number of edges pointing toward it. Use two DFS traversals: first compute the reversal count for an arbitrary root, then reroot to compute counts for all nodes in O(V+E) time. Return the minimum count.

Pro tip: Clarify that the graph may not be connected; if disconnected, no single root can reach all nodes, so the problem is infeasible. Also, mention that the optimal root is the centroid of the underlying undirected tree if the graph is a tree.

1. Understand the problem and constraints

Confirm that the graph is directed, 1-indexed, and may have cycles or be disconnected. Discuss that if disconnected, no valid root exists, so assume connected or handle separately.

2. Compute initial reversal count

Pick an arbitrary root (e.g., node 1) and perform a DFS/BFS to count how many edges need to be reversed to make all edges point away from it. For each edge u->v, if v is the parent of u, it needs reversal.

3. Reroot to compute counts for all nodes

Use the rerooting technique: when moving the root from u to its child v, the reversal count changes by +1 if the edge u->v exists (since it now points toward the new root), and -1 if the edge v->u exists. Update counts accordingly.

4. Find the minimum reversal count

After computing reversal counts for all nodes, return the minimum value. If the graph is disconnected, return -1 or indicate impossibility.

Key Points to Mention

  • Graph representation using adjacency lists with edge directions.
  • DFS/BFS for initial traversal and counting reversals.
  • Rerooting dynamic programming technique to avoid O(V*(V+E)) brute force.
  • Time complexity O(V+E) and space complexity O(V+E).
  • Handling disconnected graphs: no valid root exists.
  • Edge cases: single node, tree, cyclic graph.

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