FastPrepValidate Binary Search Tree
Problems/Validate Binary Search Tree

Validate Binary Search Tree

InMobi logoInMobi● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an InMobi onsite interview. The report gives the strict rules for a binary search tree and the intended approach with subtree minimums and maximums. It does not give an example you can run or a function signature. Our version is about 98% the same as the reported question.

Given the root of a binary tree, return true if it is a valid binary search tree and false otherwise.

Check every node. Every value in its left subtree must be strictly smaller than the node's value. Every value in its right subtree must be strictly greater. Both subtrees must follow the same rules. So duplicate values make the tree invalid.

The tree is given as a list of values, read level by level from left to right. A null marks a missing child.

Function

validateBST(root: TreeNode) → boolean

Examples

Example 1

root = [4,2,5,1,3]return = true
root
42135

Node 2 has a smaller left child, 1, and a larger right child, 3. The whole left subtree is smaller than the root 4. Node 5 on the right is larger than 4. So the answer is true.

Example 2

root = [5,1,7,null,null,4,8]return = false
root
51748

Node 4 sits in the right subtree of the root 5. Every value on the root's right must be larger than the root, but this value is smaller. So the answer is false.

Constraints

  • 1 <= number of nodes <= 5000
  • -10^6 <= node.val <= 10^6

More InMobi Onsite Interview questions

  • Russian Doll EnvelopesONSITE INTERVIEW · Seen Jul 2026
  • Minimum and Maximum with Optimal ComparisonsONSITE INTERVIEW · Seen Dec 2025
  • Minimum Edges to Connect All ComponentsONSITE INTERVIEW · Seen Dec 2025
  • Shortest Path Visiting All NodesONSITE INTERVIEW · Seen Sep 2025
  • Maximum Sum of Non-Adjacent Tree NodesONSITE INTERVIEW · Seen Jan 2022
  • Word SquaresONSITE INTERVIEW · Seen Jan 2022
  • Maximum Length Pair ChainONSITE INTERVIEW · Seen Jul 2021
  • Top View of a Binary TreeONSITE INTERVIEW · Seen Jul 2021
  • Maximum XOR Between Two ArraysONSITE INTERVIEW · Seen May 2021
  • Trapping Rain WaterONSITE INTERVIEW · Seen May 2021

Similar questions at other companies

  • Filesystem Entity Total SizeGoogle · ONSITE INTERVIEWGoogle interview questions
  • Render a Comment HierarchyAmazon · ONSITE INTERVIEW
  • Kth Smallest Element in a Binary Search TreeSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Flatten and Reconstruct an Ordered TreeThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
  • Inorder, Preorder and Postorder TraversalsAMD · ONSITE INTERVIEWAMD interview questions
  • Compare N-Ary Tree Leaf SequencesNavan · ONSITE INTERVIEWNavan interview questions
  • Flatten JSON into Repeated Key-Value PairsKlaviyo · ONSITE INTERVIEWKlaviyo interview questions
  • Kth Smallest Value in a BSTExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
  • Serialize a Binary Search TreeConfluent · ONSITE INTERVIEWConfluent interview questions
All InMobi interview questionsSee InMobi 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 boolean validateBST(TreeNode root) {
        // write your code here
    }
}
Checking account…