Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Is Binary Tree a BST
00:00
5 left

Is Binary Tree a BST

MediumPython

Problem

Explain how you determine whether a given binary tree is a binary search tree and outline an efficient approach.

Implement is_valid_bst(root), where each node is represented by an object with integer value and nullable left and right child fields. Return true only when every left descendant is strictly smaller and every right descendant is strictly larger than its ancestors. An empty tree is valid.

Constraints

  • The tree contains 0 to 10^4 nodes.
  • Each node value is an integer between -10^9 and 10^9.
  • The BST ordering is strict, so duplicate values are invalid.
  • Each node has nullable left and right child fields.

Function Signature

def is_valid_bst(root):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.