← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Uber SWE coding round, one question on BSTs. Pretty standard stuff but they pushed on complexity at the end which I wasn't fully ready for.

Questions Asked (1)

Q1

Given the root of a binary search tree and an integer k, find the value of the kth smallest element in the tree.

Algorithms & Data Structures
Author's notes

Knew immediately it was an in-order traversal thing since BST in-order gives you sorted order.

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, counting until you reach the kth node. Alternatively, if the tree is large and k is small, use an iterative stack-based traversal to avoid recursion overhead and allow early termination.

Pro tip: Mention that you can optimize by storing the size of each subtree in the nodes (if modifications are allowed) to achieve O(log n) time, but clarify that this requires extra space and preprocessing. This shows awareness of trade-offs.

1. Clarify requirements and constraints

Ask about tree size, whether k is guaranteed valid, and if the tree can be modified. This helps choose the right approach.

2. Choose traversal method

Decide between recursive in-order traversal (simple but O(n) space for recursion) and iterative stack-based traversal (O(h) space, early termination).

3. Implement in-order traversal

Write code to traverse the BST in ascending order, keeping a counter. Stop when the counter equals k and return the current node's value.

4. Analyze complexity

State time complexity O(n) worst-case, but O(k) if early termination; space complexity O(h) for iterative, O(n) for recursive.

5. Discuss optimizations

Mention augmenting the tree with subtree sizes to achieve O(log n) time if frequent queries are expected.

Key Points to Mention

  • In-order traversal of BST yields sorted order.
  • Iterative traversal with a stack allows early stopping after k elements.
  • Time complexity: O(k) with early termination, O(n) worst-case.
  • Space complexity: O(h) for iterative, O(n) for recursive due to call stack.
  • Augmenting nodes with subtree sizes can optimize to O(log n) per query.
  • Handle edge cases: k <= 0, k > number of nodes, empty tree.

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