← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Uber SWE interview with a graph traversal problem. The question looked like a math problem at first glance but it's really just weighted graph search once you see it.

Questions Asked (1)

Q1

Given a set of division equations like A / B = k, and a list of queries asking for the value of C / D, return the computed result for each query or -1.0 if it can't be determined from the given equations.

Algorithms & Data Structures
Author's notes

Took me an embarrassingly long time to stop thinking about it as algebra and realize it's just a weighted directed graph where each equation is an edge.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the equations as a weighted directed graph where each variable is a node and each equation A / B = k creates edges A→B with weight k and B→A with weight 1/k. For each query, perform a graph traversal (DFS or BFS) from the numerator to the denominator, multiplying edge weights along the path; if no path exists, return -1.0.

Pro tip: During traversal, cache results for visited nodes or use union-find with weights to optimize multiple queries, and always handle edge cases like unknown variables or self-division (C / C = 1.0) explicitly.

1. Build the graph

Create an adjacency list where each node is a variable. For each equation A / B = k, add a directed edge from A to B with weight k, and from B to A with weight 1/k.

2. Process each query

For a query C / D, if either C or D is not in the graph, return -1.0. If C equals D, return 1.0. Otherwise, perform a DFS or BFS from C to D.

3. Traverse and compute

During traversal, keep track of the cumulative product of edge weights. When D is reached, return the product; if traversal ends without reaching D, return -1.0.

4. Optimize (optional)

If there are many queries, consider caching results for visited node pairs or using union-find with weights to answer queries in near-constant time after preprocessing.

Key Points to Mention

  • Graph representation: variables as nodes, division equations as weighted directed edges.
  • DFS/BFS traversal to find a path and compute the product of weights.
  • Handling of edge cases: unknown variables, self-division, disconnected components.
  • Time complexity: O(E + Q * (V + E)) for naive approach, or O(E + Q * α(V)) with union-find.
  • Space complexity: O(V + E) for graph storage.
  • Potential optimizations: caching, union-find with weights, or Floyd-Warshall for dense graphs.

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