Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Validate if Tree is BST in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of BST Validation on a Simple Tree
What is the output of the following code that checks if a binary tree is a BST?
DSA Javascript
class Node {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isBST(root, min = -Infinity, max = Infinity) {
  if (!root) return true;
  if (root.val <= min || root.val >= max) return false;
  return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}

const tree = new Node(10, new Node(5), new Node(15, new Node(6), new Node(20)));
console.log(isBST(tree));
Afalse
BSyntaxError
CTypeError
Dtrue
Attempts:
2 left
💡 Hint
Check if all nodes in the left subtree are less than the root and all nodes in the right subtree are greater.
❓ Predict Output
intermediate
2:00remaining
Result of BST Validation on a Perfect BST
What will be printed after running this code that validates a perfect BST?
DSA Javascript
class Node {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isBST(root, min = -Infinity, max = Infinity) {
  if (!root) return true;
  if (root.val <= min || root.val >= max) return false;
  return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}

const tree = new Node(8, new Node(3, new Node(1), new Node(6)), new Node(10, null, new Node(14)));
console.log(isBST(tree));
Afalse
Btrue
CReferenceError
Dundefined
Attempts:
2 left
💡 Hint
All left children are less and right children are greater than their parent nodes.
🔧 Debug
advanced
2:00remaining
Identify the Error in BST Validation Code
What error will this code produce when checking if a tree is a BST?
DSA Javascript
class Node {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isBST(root, min = -Infinity, max = Infinity) {
  if (!root) return true;
  if (root.val <= min || root.val >= max) return false;
  return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}

const tree = new Node(10, new Node(5), new Node(15));
console.log(isBST(tree));
Afalse
BSyntaxError
Ctrue
DLogic error: returns false for valid BST
Attempts:
2 left
💡 Hint
Check the comparison operators in the condition that returns false.
🧠 Conceptual
advanced
2:00remaining
Understanding BST Validation Limits
Why is it necessary to pass min and max values when validating if a tree is a BST?
ATo store the sum of all node values
BTo count the number of nodes in the tree
CTo keep track of the allowed value range for each node during recursion
DTo balance the tree during validation
Attempts:
2 left
💡 Hint
Think about how BST rules apply to all descendants, not just immediate children.
🚀 Application
expert
3:00remaining
Output of BST Validation on a Large Tree with Duplicate Values
Given the following tree with duplicate values, what will the BST validation function return?
DSA Javascript
class Node {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isBST(root, min = -Infinity, max = Infinity) {
  if (!root) return true;
  if (root.val <= min || root.val >= max) return false;
  return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}

const tree = new Node(10,
  new Node(5,
    new Node(3),
    new Node(7, new Node(7), null)
  ),
  new Node(15, null, new Node(20))
);
console.log(isBST(tree));
Afalse
Btrue
CRangeError
DTypeError
Attempts:
2 left
💡 Hint
BSTs do not allow duplicate values on the same side; check the duplicate 7 in the left subtree.

Practice

(1/5)
1. What does it mean for a binary tree to be a Binary Search Tree (BST)?
easy
A. Every node has exactly two children
B. Every node's left child is smaller and right child is larger than the node
C. All nodes have the same value
D. The tree is balanced with equal height on both sides

Solution

  1. Step 1: Understand BST property

    A BST requires that for each node, all nodes in the left subtree have smaller values and all nodes in the right subtree have larger values.
  2. Step 2: Compare options with definition

    Every node's left child is smaller and right child is larger than the node correctly states this property. Other options describe different tree properties.
  3. Final Answer:

    Every node's left child is smaller and right child is larger than the node -> Option B
  4. Quick Check:

    BST property = Every node's left child is smaller and right child is larger than the node [OK]
Hint: BST means left < node < right for every node [OK]
Common Mistakes:
  • Confusing BST with balanced tree
  • Thinking all nodes must have two children
  • Assuming all values are equal
2. Which of the following JavaScript function signatures is correct to check if a binary tree is a BST?
easy
A. function isBST(node, min, max) {}
B. function isBST(node, left, right, parent) {}
C. function checkBST(root) {}
D. function validateBST(tree, minVal) {}

Solution

  1. Step 1: Identify common BST validation parameters

    To validate BST, we usually pass the current node and allowed min and max values to check constraints.
  2. Step 2: Match function signatures

    function isBST(node, min, max) {} matches this pattern with node, min, and max parameters. Others have extra or missing parameters.
  3. Final Answer:

    function isBST(node, min, max) {} -> Option A
  4. Quick Check:

    Correct BST function signature = function isBST(node, min, max) {} [OK]
Hint: Use min and max limits to check BST recursively [OK]
Common Mistakes:
  • Adding unnecessary parameters
  • Missing min or max limits
  • Using wrong parameter names
3. What will be the output of the following code snippet?
class Node {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isBST(node, min = -Infinity, max = Infinity) {
  if (!node) return true;
  if (node.val <= min || node.val >= max) return false;
  return isBST(node.left, min, node.val) && isBST(node.right, node.val, max);
}

const root = new Node(10, new Node(5), new Node(15, new Node(6), new Node(20)));
console.log(isBST(root));
medium
A. undefined
B. true
C. false
D. TypeError

Solution

  1. Step 1: Analyze tree structure

    Root is 10, left child 5 (valid), right child 15 with left child 6 and right child 20.
  2. Step 2: Check BST property on right subtree

    Node 6 is left child of 15 but 6 < 10, which violates BST property because all right subtree nodes must be > 10.
  3. Final Answer:

    false -> Option C
  4. Quick Check:

    6 in right subtree < 10 breaks BST = false [OK]
Hint: Check if left < node < right recursively [OK]
Common Mistakes:
  • Ignoring subtree value constraints
  • Assuming all children just compare with parent
  • Missing base case for null nodes
4. Find the bug in this BST validation function and select the fix:
function isBST(node) {
  if (!node) return true;
  if (node.left && node.left.val >= node.val) return false;
  if (node.right && node.right.val <= node.val) return false;
  return isBST(node.left) && isBST(node.right);
}
medium
A. Add min and max parameters to track value ranges
B. Change >= to > and <= to < in comparisons
C. Return false if node is null
D. Remove recursive calls to isBST

Solution

  1. Step 1: Understand current checks

    Function only compares immediate children with current node, missing subtree constraints.
  2. Step 2: Identify fix

    To correctly validate BST, function must track allowed min and max values for each subtree, not just immediate children.
  3. Final Answer:

    Add min and max parameters to track value ranges -> Option A
  4. Quick Check:

    Use min/max limits to validate BST correctly [OK]
Hint: Use min/max limits, not just immediate child values [OK]
Common Mistakes:
  • Only checking immediate children
  • Ignoring subtree value constraints
  • Not using recursion properly
5. Given a binary tree where nodes can have duplicate values, which modification to the BST validation logic allows duplicates only on the right subtree? Choose the correct condition to replace node.val <= min and node.val >= max checks in the recursive function.
hard
A. Allow duplicates by changing left subtree check to node.val < min and right subtree check to node.val > max
B. Allow duplicates on right by changing left subtree check to node.val <= min and right subtree check to node.val > max
C. Allow duplicates on right by changing left subtree check to node.val <= min and right subtree check to node.val >= max
D. Allow duplicates on right by changing left subtree check to node.val < min and right subtree check to node.val >= max

Solution

  1. Step 1: Understand duplicates in BST

    Allowing duplicates only on right means left subtree nodes must be strictly less than current node, right subtree nodes can be greater or equal.
  2. Step 2: Adjust min/max checks accordingly

    Left subtree check should be node.val < min (strictly less), right subtree check should be node.val >= max (allow equal).
  3. Final Answer:

    Allow duplicates on right by changing left subtree check to node.val < min and right subtree check to node.val >= max -> Option D
  4. Quick Check:

    Duplicates only right: left < node, right >= node [OK]
Hint: Strict less on left, allow equal on right for duplicates [OK]
Common Mistakes:
  • Allowing duplicates on both sides
  • Using non-strict inequalities on left subtree
  • Confusing min and max checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard