← Uber Interview Insights

Uber·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Uber SWE coding round, two problems back to back. The first was a tree construction problem and the second was a graph traversal thing. Not the hardest session I've had but the second problem took me longer than I expected.

Questions Asked (2)

Q1

Given an N x N matrix representing an image where N is a power of 2, design a quadtree node structure and write a function to build the quadtree. Leaf nodes store a single value when all pixels in a region are identical; otherwise the node splits into four child quadrants.

Algorithms & Data StructuresData Modeling
Author's notes

I actually liked this one.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining a Quadtree node class with fields for value, isLeaf, and children. Then implement a recursive build function that checks if all pixels in the current region are identical; if so, create a leaf node, otherwise split into four quadrants and recurse. Analyze time and space complexity, noting O(N^2) time and O(N^2) space in the worst case.

Pro tip: Mention that the quadtree is useful for image compression and that the recursion depth is O(log N), which is efficient for large N. Also, discuss how to handle non-power-of-2 sizes by padding, showing awareness of practical constraints.

1. Define the node structure

Create a class with fields: val (the pixel value if leaf), isLeaf (boolean), and children (array of four Quadtree nodes). Optionally include topLeftRow, topLeftCol, and size for clarity.

2. Design the recursive build function

Write a function build(grid, row, col, size) that checks if all pixels in the region are the same. If yes, return a leaf node with that value; otherwise, split into four quadrants of size/2 and recursively build each child.

3. Implement the uniformity check

Iterate through the region to check if all values are identical. Optimize by early termination when a mismatch is found.

4. Handle base cases and recursion

If size == 1, always return a leaf node. Ensure recursion terminates and correctly assigns children.

5. Analyze complexity and edge cases

Discuss time complexity: O(N^2) in worst case (all pixels different) and O(1) if all same. Space complexity: O(N^2) worst case. Mention edge cases like N=1 or non-power-of-2 (if allowed).

Key Points to Mention

  • Quadtree node structure with isLeaf, val, and children array of size 4.
  • Recursive divide-and-conquer approach: check uniformity, then split into four quadrants.
  • Time complexity: O(N^2) worst case, O(1) best case; space complexity: O(N^2) worst case.
  • Use of early termination in uniformity check to optimize.
  • Recursion depth is O(log N) due to halving size each level.
  • Practical applications: image compression, spatial indexing, and collision detection.

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

Q2

Given a directed graph as an adjacency list, find all 'safe' nodes where every path from that node eventually terminates at a node with no outgoing edges. Return them in ascending order.

Algorithms & Data Structures
Author's notes

This one got me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as finding nodes that are not part of any cycle and cannot reach a cycle. Use a reverse graph and topological sort (Kahn's algorithm) to iteratively remove nodes with out-degree zero in the original graph, or use DFS with cycle detection to mark unsafe nodes.

Pro tip: Clarify edge cases upfront: empty graph, self-loops, and multiple components. Mention that the reverse graph approach avoids recursion depth issues and is O(V+E).

1. Understand the problem

Restate that a safe node is one from which every path eventually reaches a terminal node (out-degree 0). Nodes that can reach a cycle are unsafe.

2. Choose an approach

Decide between DFS with cycle detection (three-color marking) or reverse graph + topological sort (Kahn's algorithm). Both are O(V+E).

3. Implement the algorithm

For DFS: mark nodes in current recursion stack as unsafe; propagate unsafe status to predecessors. For Kahn's: build reverse graph, compute out-degrees, enqueue nodes with out-degree 0, and process.

4. Collect and sort results

After processing, gather all safe nodes and sort them in ascending order as required.

5. Analyze complexity and edge cases

State time and space complexity O(V+E). Discuss handling of empty graph, self-loops, and disconnected components.

Key Points to Mention

  • Definition of safe nodes: nodes from which all paths lead to terminal nodes (out-degree 0).
  • Cycle detection: nodes that are part of or can reach a cycle are unsafe.
  • DFS with three-color marking (white, gray, black) to detect cycles and mark unsafe nodes.
  • Reverse graph + topological sort (Kahn's algorithm) to iteratively remove nodes with out-degree 0.
  • Time and space complexity: O(V+E) for both approaches.
  • Edge cases: empty graph, self-loops, multiple components, and nodes with no outgoing edges.

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