Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Validate BST Preorder
00:00
5 left

Validate BST Preorder

MediumPython

Problem

Check if a given array can represent a preorder traversal of a BST and return true or false.

Implement can_represent_bst_preorder(preorder). The input is a list of integers, and the function must return a boolean. Assume the BST contains distinct values, so duplicate values make the traversal invalid. An empty array represents an empty BST and is valid.

Constraints

  • 0 <= preorder.length <= 1000
  • -10^9 <= preorder[i] <= 10^9
  • The BST uses distinct integer values

Function Signature

def can_represent_bst_preorder(preorder):
Interviewer

Your question is Validate BST Preorder. 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.