← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Uber SWE interview with a BST problem that seemed straightforward until the follow-up made me realize I hadn't thought about the dynamic case at all.

Questions Asked (1)

Q1

Given the root of a binary search tree and an integer k, return the kth smallest value in the tree (1-indexed).

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

In-order traversal gives you a sorted sequence, so I just did that and counted up to k.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use an in-order traversal of the BST to visit nodes in ascending order, keeping a counter to stop at the kth node. This yields O(k) time and O(h) space, which is optimal for a single query. If multiple queries are expected, discuss augmenting nodes with subtree sizes for O(log n) per query.

Pro tip: Mention that in-order traversal can be done iteratively with a stack to avoid recursion overhead and to handle large trees gracefully. Also, clarify the 1-indexed nature of k and handle edge cases like k > number of nodes.

1. Clarify the problem and constraints

Confirm that k is 1-indexed, the tree is a valid BST, and discuss edge cases such as k being larger than the number of nodes. Ask if multiple queries will be made to decide between simple traversal and augmented tree.

2. Choose the traversal method

Decide between recursive and iterative in-order traversal. Iterative is often preferred for its explicit stack and early termination, but recursive is simpler if the tree depth is manageable.

3. Implement the in-order traversal with a counter

Traverse the tree in-order, incrementing a counter at each node. When the counter equals k, return the node's value immediately to avoid unnecessary traversal.

4. Analyze time and space complexity

Explain that time complexity is O(k) in the best case (early stop) and O(n) in the worst case, while space is O(h) for the stack, where h is the tree height.

5. Discuss follow-up optimizations

If multiple queries are expected, propose augmenting each node with the size of its left subtree to find the kth smallest in O(log n) time per query.

Key Points to Mention

  • In-order traversal of a BST yields nodes in sorted order.
  • Use a counter to track the number of nodes visited and stop early when reaching k.
  • Iterative traversal with a stack avoids recursion depth issues and allows early termination.
  • Time complexity: O(k) best case, O(n) worst case; space complexity: O(h) for stack.
  • Edge cases: k <= 0, k > number of nodes, empty tree.
  • For multiple queries, augment nodes with subtree sizes to achieve O(log n) per query.

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