← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Uber SWE interview with a BST problem that seemed straightforward until the follow-up hit. The algorithmic part was fine but the optimization discussion is where things got interesting.

Questions Asked (2)

Q1

Given the root of a binary search tree and an integer k, return the k-th smallest value in the tree. Expected an O(H + k) iterative solution using in-order traversal with a stack.

Algorithms & Data Structures
Author's notes

I went recursive first out of habit and they nudged me toward iterative.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem and constraints, then explain that an iterative in-order traversal using a stack naturally yields nodes in sorted order. Describe how to simulate recursion with a stack, pushing left children until null, then popping and checking if the k-th node is reached, decrementing k each time. Emphasize that this achieves O(H + k) time and O(H) space, which is optimal for this problem.

Pro tip: Mention that you can stop early once k reaches zero, avoiding unnecessary traversal of the entire tree, and note that this approach is robust for large trees where recursion depth might cause stack overflow.

1. Clarify requirements and constraints

Confirm that the tree is a valid BST, k is 1-indexed, and that k is within the number of nodes. Ask about edge cases like empty tree or k out of range.

2. Choose the optimal traversal strategy

Explain that in-order traversal of a BST visits nodes in ascending order, so the k-th visited node is the answer. Compare recursive vs iterative and justify iterative for O(H + k) time and O(H) space.

3. Design the iterative algorithm with a stack

Initialize an empty stack and a pointer to the root. Loop while stack is not empty or current node is not null: push all left descendants, then pop the top node, decrement k, and if k==0 return its value; otherwise move to its right child.

4. Analyze complexity and edge cases

State that time complexity is O(H + k) because we push at most H nodes before finding the k-th, and space is O(H) for the stack. Handle edge cases: empty tree, k <= 0, or k > number of nodes by returning -1 or throwing an exception.

5. Test with examples and discuss optimizations

Walk through a small BST example to verify correctness. Mention that if the tree is modified frequently, augmenting nodes with subtree sizes could allow O(H) search, but for a static tree the stack approach is optimal.

Key Points to Mention

  • In-order traversal of a BST yields nodes in sorted order.
  • Iterative stack-based traversal simulates recursion and avoids call stack overhead.
  • Time complexity O(H + k) and space complexity O(H), where H is tree height.
  • Early termination when k reaches zero saves unnecessary traversal.
  • Handling edge cases: empty tree, k out of bounds, and skewed trees.
  • Potential follow-up: using augmented BST with subtree sizes for O(H) k-th smallest.

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

Q2

Follow-up: if the BST is modified frequently with inserts and deletes, and you also need to query the k-th smallest element frequently, how would you redesign the approach?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I got a little lost.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Acknowledge that a plain BST gives O(h) for k-th smallest, which degrades with frequent updates. Propose augmenting each node with subtree size to enable O(log n) select, and discuss balancing (e.g., AVL/Red-Black) to maintain logarithmic height. If updates and queries are both very frequent, consider a Fenwick tree over compressed values or an order-statistic tree, and analyze trade-offs.

Pro tip: Mention that in practice, a Fenwick tree with coordinate compression often outperforms augmented BSTs due to lower constant factors and simpler implementation, but it requires knowing all values in advance or handling online updates carefully.

1. Identify the bottleneck

Explain that frequent inserts/deletes can unbalance a plain BST, making k-th smallest queries O(n) in the worst case. The goal is to maintain O(log n) for both updates and queries.

2. Augment nodes with subtree size

Store the size of the subtree at each node. This allows finding the k-th smallest in O(log n) by comparing k with the size of the left subtree.

3. Ensure balancing

Use a self-balancing BST (e.g., AVL, Red-Black, or a weight-balanced tree) to guarantee O(log n) height after updates. Update subtree sizes during rotations.

4. Consider alternative data structures

If updates and queries are both very frequent, evaluate a Fenwick tree (BIT) over compressed values for O(log n) updates and queries, or an order-statistic tree. Discuss trade-offs like offline vs online, memory, and implementation complexity.

5. Analyze trade-offs and conclude

Compare the approaches in terms of time complexity, constant factors, and ease of implementation. Recommend the best fit for the given scenario, possibly mentioning hybrid or advanced structures like a treap or skip list.

Key Points to Mention

  • Subtree size augmentation for order statistics
  • Self-balancing BSTs (AVL, Red-Black) to maintain O(log n) height
  • Fenwick tree (BIT) with coordinate compression for O(log n) updates and queries
  • Order-statistic tree as a direct solution
  • Trade-offs: online vs offline, memory overhead, implementation complexity
  • Constant factors: Fenwick trees often faster in practice despite similar asymptotic complexity

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