Question presented to candidate: "You are given the root of a binary tree. Write a Python function that returns true if it is a valid binary search tree โ every key in a node's left subtree is strictly smaller than the node, and every key in its right subtree is strictly larger."
What a strong answer should cover:
- Why comparing each node only with its two children is wrong (a deep descendant can break the rule), with a counter-example.
- The range method: pass down an open interval (low, high) that tightens at every step; O(n) time, O(h) space.
- The in-order method: an in-order walk of a BST is strictly increasing, so compare each value with the previous one and stop at the first drop.
- Duplicates: with the strict definition, equal keys make the tree invalid.
- Python specifics: use
None(orfloat('-inf')/float('inf')) for "no bound", and prefer the iterative in-order version on deep trees because recursion stops at about 1000 frames by default.
Clarifying questions expected:
- Are duplicate values allowed, and if so on which side?
- How deep can the tree be (is recursion safe)?
Code / implementation expected: Yes โ a recursive range check or an iterative in-order check with a list as the stack, in Python 3.