← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Uber SWE interview with a tree problem that looks straightforward until you realize the path can go through a parent in both directions. Took me a bit to wrap my head around the child-parent-child constraint.

Questions Asked (1)

Q1

Given the root of a binary tree, find the length of the longest consecutive path where adjacent node values differ by exactly one. The path can be increasing or decreasing, and it can pass through a node in the child-parent-child direction.

Algorithms & Data Structures
Author's notes

My first instinct was to treat it like the simpler version where the path only goes parent to child.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order DFS that returns the longest consecutive path starting at the current node, considering both increasing and decreasing sequences. At each node, combine the best child paths that continue the sequence (value differs by 1) to update a global maximum, allowing the path to pass through the node.

Pro tip: Clarify that the path can go through a node in a child-parent-child direction, meaning you need to consider both children simultaneously to form a longer path. Also, mention that you can optimize space by using recursion and avoiding explicit memoization.

1. Clarify the problem

Confirm that the path can be increasing or decreasing, and that it can pass through a node connecting two children. Ensure you understand that the path length is measured in number of nodes (or edges, but typically nodes).

2. Define recursive function

Design a DFS function that returns the longest consecutive path starting at the current node, considering both increasing and decreasing sequences. It should return two values: the longest increasing path and the longest decreasing path starting at this node.

3. Process children and update global max

For each child, recursively get its increasing and decreasing path lengths. If the child's value is current+1, it can extend the increasing path; if current-1, it can extend the decreasing path. Combine the best increasing and decreasing paths from different children to form a path through the current node, and update the global maximum.

4. Handle base case and return

For a null node, return (0,0). For a leaf, return (1,1). After processing children, return the longest increasing and decreasing paths starting at the current node (1 + max from children if applicable).

5. Analyze complexity

State that the time complexity is O(n) since each node is visited once, and space complexity is O(h) for recursion stack, where h is the tree height.

Key Points to Mention

  • Post-order DFS to compute longest consecutive paths from each node
  • Maintain two values per node: longest increasing and longest decreasing path starting at that node
  • Combine paths from left and right children to form a path through the current node
  • Update a global maximum at each node
  • Time complexity O(n), space complexity O(h)
  • Handle edge cases: empty tree, single node, and paths that go through a node

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