← DoorDash Interview Insights

DoorDash·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

DoorDash coding screen, one BST problem, nothing else to report really.

Questions Asked (1)

Q1

Given the root of a non-empty Binary Search Tree and a target number, find the node value in the BST that is closest to the target.

Algorithms & Data Structures
Author's notes

BST traversal question.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use the BST property to guide a search toward the target, keeping track of the closest value seen so far. At each node, update the closest if the current node is nearer to the target, then move left if the target is smaller or right if larger. Continue until you reach a null child, returning the closest value.

Pro tip: Mention that the iterative approach uses O(1) space and avoids recursion overhead, which is often preferred in production code. Also, clarify how you handle ties (e.g., if two nodes are equally close, return either or specify a rule).

1. Clarify the problem

Confirm that the BST is non-empty, the target can be any number (including outside the range of values), and ask if ties should be broken in a specific way.

2. Initialize tracking variables

Set a variable to store the closest value, initially the root's value, and a pointer to traverse the tree starting at the root.

3. Traverse the BST

While the current node is not null, compare the absolute difference between the node's value and the target with the current closest difference; update the closest if the node is closer. Then move left if the target is less than the node's value, otherwise move right.

4. Return the closest value

Once the traversal ends (current node becomes null), return the stored closest value.

5. Analyze complexity

State that the time complexity is O(h) where h is the height of the tree (O(log n) for balanced BST, O(n) worst-case), and space complexity is O(1) for the iterative approach.

Key Points to Mention

  • Leveraging the BST property to decide direction (left for smaller, right for larger).
  • Maintaining the closest value seen so far and updating it at each node.
  • Handling edge cases: target smaller than all nodes, larger than all nodes, or exactly matching a node.
  • Time complexity O(h) and space complexity O(1) for iterative solution.
  • Alternative recursive solution and its space complexity O(h) due to call stack.
  • Potential tie-breaking rule if two nodes are equally close (e.g., return the smaller value).

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