Validate string valid BST

Last updated: June 5, 2026

Quick Overview

Given a binary tree, implement a function to determine if the tree is a valid binary search tree (BST). A valid BST is defined as a tree where for each node, all values in the left subtree are less than the node's value, and all values in the right subtree are greater. The function should return a boolean value: true if the tree is a valid BST, and false otherwise.

Compass
Coding & Algorithms
Software Engineer
Compass
June 5, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

10

8

689 solved


Given a binary tree, implement a function to determine if the tree is a valid binary search tree (BST). A valid BST is defined as a tree where for each node, all values in the left subtree are less than the node's value, and all values in the right subtree are greater. The function should return a boolean value: true if the tree is a valid BST, and false otherwise.

Coding interviews at Compass focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Graph algorithms and traversal
Time and space complexity analysis
Binary search and divide and conquer
Hash maps and frequency counting
Tree structures and recursion
Edge cases and input validation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • What if the input doesn't fit in memory?
  • How would you modify your solution to handle streaming input?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

To determine if a binary tree is a valid binary search tree (BST), we can utilize the properties of BSTs: for every node, the values in its left subtree must be less than the node's value, and the val...

Approach
  1. Define the recursive function: The function will take a node and two bounds (lower and upper) that define the valid range for that node's value.
  2. Base case: If the node is null, return t...

Submit Your Answer
Markdown supported

Related Questions