← Upstart Interview Insights

Upstart·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

Did a technical screen for a software engineer role at Upstart and got a BST problem. Pretty standard algorithmic stuff but the edge case conversation went longer than I expected.

Questions Asked (1)

Q1

Given the root of a binary search tree and an integer k, return the k-th smallest value in the tree. How do you handle edge cases like an empty tree or k being out of bounds?

Algorithms & Data Structures
Author's notes

Went with in-order traversal pretty quickly 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 k-th node. For efficiency, implement an iterative traversal with a stack to avoid recursion overhead and allow early termination. Explicitly handle edge cases: if the tree is empty or k is less than 1 or greater than the number of nodes, return an appropriate error or sentinel value.

Pro tip: Mention that you can optimize by augmenting each node with the size of its left subtree, enabling O(log n) search for the k-th smallest in a balanced BST. Also, clarify with the interviewer what to return for invalid k (e.g., null, -1, or throw an exception) to align with expectations.

1. Clarify assumptions and edge cases

Ask the interviewer about the expected return value for invalid inputs (empty tree, k out of bounds) and whether k is 1-indexed. Confirm if the tree can be modified or if additional data structures are allowed.

2. Choose traversal strategy

Decide between recursive and iterative in-order traversal. Iterative is preferred for early termination and avoiding stack overflow; recursive is simpler but may traverse the entire tree.

3. Implement in-order traversal with counter

Traverse the tree in-order, incrementing a counter for each visited node. When the counter equals k, return the current node's value. If traversal completes without reaching k, handle the out-of-bounds case.

4. Handle edge cases explicitly

Check if the root is null and return the agreed-upon value. Validate k: if k <= 0 or k > total nodes, return the agreed-upon error value. Optionally, compute total nodes first if needed.

5. Analyze complexity and discuss optimizations

State time complexity O(h + k) for iterative in-order (h is height) and space O(h). Mention that augmenting nodes with subtree sizes can achieve O(log n) for balanced trees, but requires extra space and maintenance.

Key Points to Mention

  • In-order traversal of a BST yields nodes in ascending order.
  • Iterative traversal with a stack allows early termination and avoids recursion limits.
  • Edge cases: empty tree (return null/error), k <= 0 or k > number of nodes (return null/error).
  • Time complexity: O(h + k) for iterative in-order, where h is tree height; space O(h).
  • Optimization: augment nodes with left subtree size for O(log n) search in balanced BST.
  • Clarify with interviewer the expected behavior for invalid k and whether k is 1-indexed.

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