FastPrepFind a Value in a Binary Search Tree
Problems/Find a Value in a Binary Search Tree

Find a Value in a Binary Search Tree

Capital One logoCapital One● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2018 Capital One intern report from Blacksburg lists one technical question from its three interview panels. The question is to build a search in a binary search tree.

It does not say what the search returns. Here it returns the node that holds the value together with everything below it, or an empty tree when the value is missing. Trees have up to 500 nodes with distinct values. These are our choices.

Our version is about 80% the same as the reported question.

root is a binary search tree with distinct values: at every node, all values in the subtree under its left child are smaller than the node's value, and all values in the subtree under its right child are larger.

Find the node whose value is val and return it, which returns its whole subtree. If no node holds val, return an empty tree.

Function

searchBST(root: TreeNode, val: int) → TreeNode

Examples

Example 1

root = [15,8,23,4,11,null,30,null,6]val = 8return = [8,4,11,null,6]
root
15846112330
Removed
→
return
84611
Moved or new

8 is smaller than the root 15, so go left and find it right away. The answer is 8 with everything below it: 4, 11 and 6.

Example 2

root = [15,8,23,4,11,null,30,null,6]val = 9return = []
root
15846112330
return
empty

From 15 go left to 8, then right to 11, then left, which is empty. 9 is not in the tree, so the answer is empty.

Example 3

root = [7]val = 7return = [7]
root
7
→
return
7

The only node holds the value, so the whole tree is the answer.

Constraints

  • The number of nodes in the tree is between 1 and 500.
  • 1 <= Node.val, val <= 10^7
  • root is a binary search tree with distinct values.

More Capital One Onsite Interview questions

  • Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
  • Sort ColorsONSITE INTERVIEW · Seen Oct 2026
  • Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
  • Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
  • Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
  • Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
  • Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
  • Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
  • Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025

Similar questions at other companies

  • Insert into a Left-Complete Binary TreeRubrik · ONSITE INTERVIEWRubrik interview questions
  • Kth Closest Element in a BSTLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Shortest Path Between BST NodesGoogle · ONSITE INTERVIEWGoogle interview questions
  • Repeated BST Range Sum and AverageMeta · ONSITE INTERVIEWMeta interview questions
  • Lowest Common Ancestor in a BSTPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
  • Delete Node in a BSTBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Validate Binary Search TreeTokopedia · ONSITE INTERVIEWTokopedia interview questions
  • Build a Perfect Binary Search TreeExpedia · ONSITE INTERVIEWExpedia interview questions
  • Next Larger Value in a BSTMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Lowest Common Ancestor in an Inserted BSTAmazon · PHONE SCREEN

Capital One interview experiences

  • Capital One Coding OA Questions: 3 Experiences
  • Capital One Experienced Engineer Power Day and OA: 6 Experiences
  • Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
  • Capital One TDP Agentic Round (AI Coding): 7 Experiences
  • Capital One SWE Power Day, Level Not Reported: 6 Experiences
  • Capital One TIP Intern OA and Power Day: 13 Experiences
All Capital One interview questionsSee Capital One hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        // Write your code here.
    }
}
Checking account…