Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Range Sum of BST
00:00
5 left

Range Sum of BST

EasyPython

Problem

Meta's News Feed infrastructure frequently works with ordered numeric attributes such as timestamps or priority scores. Given the root of a binary search tree and two bounds, return the sum of all node values in the inclusive range [low, high].

Use the BST ordering property to avoid exploring subtrees that cannot contain valid values.

Formal Specification

Implement range_sum_bst(root, low, high), where root is a binary tree node, and low and high are integers. Each node has an integer val and optional left and right children. The evaluator constructs the tree from the provided level-order representation, using null for missing children. Return an integer.

The tree may be empty. All node values are unique, and low <= high.

Constraints

  • 0 <= number of nodes <= 2 * 10^4
  • -10^5 <= node.val, low, high <= 10^5
  • The input is a valid binary search tree
  • low <= high
  • Node values are unique

Function Signature

def range_sum_bst(root, low, high):
Interviewer

Your question is Range Sum of BST. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

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.