Challenge - 5 Problems
BST Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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));
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.
✗ Incorrect
The node with value 6 is in the right subtree of 10 but less than 10, which violates BST rules, so the function returns false.
❓ Predict Output
intermediate2: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));
Attempts:
2 left
💡 Hint
All left children are less and right children are greater than their parent nodes.
✗ Incorrect
The tree satisfies BST properties at every node, so the function returns true.
🔧 Debug
advanced2: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));
Attempts:
2 left
💡 Hint
Check the comparison operators in the condition that returns false.
✗ Incorrect
The code uses < and > instead of <= and >=, so it wrongly rejects valid BST nodes equal to min or max.
🧠 Conceptual
advanced2:00remaining
Understanding BST Validation Limits
Why is it necessary to pass min and max values when validating if a tree is a BST?
Attempts:
2 left
💡 Hint
Think about how BST rules apply to all descendants, not just immediate children.
✗ Incorrect
Passing min and max helps ensure every node fits within the valid range defined by its ancestors, enforcing BST properties.
🚀 Application
expert3: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));
Attempts:
2 left
💡 Hint
BSTs do not allow duplicate values on the same side; check the duplicate 7 in the left subtree.
✗ Incorrect
The duplicate 7 in the left subtree violates the strict less-than rule for left children, so the function returns false.