FastPrepValidate Binary Search Tree
Problems/Validate Binary Search Tree

Validate Binary Search Tree

Kotak Mahindra Bank logoKotak Mahindra Bank● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In the second round of a Kotak Mahindra Bank SDE-2 loop, the candidate had to check whether a binary tree is a valid binary search tree. The report links the well-known problem and gives two examples, which are Examples 1 and 2 here. The candidate passed each node a lower and an upper limit.

The report gives no limits. Here, the tree has 1 to 2000 nodes with 32-bit values, and equal values are not allowed.

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

You are given the root of a binary tree. Return true if it is a valid binary search tree (BST), and false otherwise.

A binary tree is a valid BST when every node follows both rules:

  • Every value in the node's left subtree is strictly smaller than the node's value.
  • Every value in the node's right subtree is strictly larger than the node's value.

The rules apply to the whole subtree, not only to the direct children. Because the comparisons are strict, a tree with two equal values is never a valid BST.

Function

isValidBST(root: TreeNode) → boolean

Examples

Example 1

root = [5,1,4,null,null,3,6]return = false
root
51436

The root is 5. Its right child is 4, but every value in the right subtree must be larger than 5. So the tree is not a valid BST.

Example 2

root = [2,1,3]return = true
root
213

The left child 1 is smaller than 2, and the right child 3 is larger. Both children are leaves, so the tree is a valid BST.

Example 3

root = [5,4,6,null,null,3,7]return = false
root
54637

Each node is correct compared with its own children: 4 < 5 < 6 and 3 < 6 < 7. But 3 is in the right subtree of the root 5, and 3 is smaller than 5. So the tree is not a valid BST.

Constraints

  • The number of nodes in the tree is in the range [1, 2000].
  • -2^31 <= Node.val <= 2^31 - 1

More Kotak Mahindra Bank interview questions

  • Longest Common PrefixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Longest Consecutive SequencePHONE SCREEN · Seen Nov 2026
  • Palindrome NumberPHONE SCREEN · Seen Nov 2026
  • Roman to IntegerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Remove Duplicates From Sorted Array In PlacePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
  • Best Time to Buy and Sell Stock IIONSITE INTERVIEW · Seen Nov 2026
  • Maximum Non-Adjacent Coins in Two RowsONSITE INTERVIEW · Seen Oct 2026
  • Search in Rotated Sorted ArrayONSITE INTERVIEW · Seen Oct 2026
  • Find the Index of the First Occurrence in a StringONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • House Robber IIIOkta · PHONE SCREENOkta interview questions
  • Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
  • Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
  • House Robber IIIVisa · PHONE SCREENVisa interview questions
  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
  • Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
  • Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake interview questions
All Kotak Mahindra Bank interview questionsSee Kotak Mahindra Bank 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 isValidBST(TreeNode root) {
        // Write your code here.
    }
}
Checking account…