← Uber Interview Insights

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

Intermediate
Jun 2026

Summary

Second round at Uber for a software engineering role. One coding question plus a follow-up extension, then a tradeoff discussion to close it out.

Questions Asked (1)

Q1

Given a binary tree where each node holds a digit, compute the sum of all numbers formed by root-to-leaf paths.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Felt pretty comfortable with the base case but the follow-up threw me off.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a depth-first search (DFS) to traverse the tree, passing the current number formed so far. At each leaf, add the current number to a running sum. This approach is efficient and easy to implement recursively.

Pro tip: Clarify edge cases upfront, such as an empty tree or a tree with a single node, and discuss how to handle integer overflow if the tree is deep. This shows attention to detail and robustness.

1. Understand the problem

Confirm that each root-to-leaf path forms a number by concatenating digits, and we need the sum of all such numbers. Ask clarifying questions about tree size, digit range, and expected output type.

2. Choose traversal method

Decide between recursive DFS, iterative DFS with a stack, or BFS. DFS is natural because it maintains the path from root to current node.

3. Design recursive function

Define a helper function that takes a node and the current number formed so far. At each node, update the number as current * 10 + node.val. If it's a leaf, return the number; otherwise, return the sum of recursive calls on left and right children.

4. Handle edge cases

Check for empty tree (return 0) and single node (return its value). Consider potential integer overflow and discuss using long or modular arithmetic if needed.

5. Analyze complexity

Time complexity is O(N) where N is number of nodes, as each node is visited once. Space complexity is O(H) for recursion stack, where H is tree height, which is O(N) in worst case.

Key Points to Mention

  • Depth-first search (DFS) is ideal for path-based problems.
  • Maintain the current number by multiplying by 10 and adding the node's digit.
  • Identify leaves as nodes with no left and right children.
  • Sum the numbers at each leaf and return the total.
  • Time complexity: O(N), space complexity: O(H) due to recursion.
  • Edge cases: empty tree, single node, and integer overflow for deep trees.

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