Validate That a Cached Search Index Satisfies the BST Property
Last updated: November 29, 2026
Quick Overview
A service caches a binary search tree of document IDs. Return whether the tree is a valid BST: every node's key is strictly greater than all keys in its left subtree and strictly less than all keys in its right subtree.
0
0
0 solved
A service caches a binary search tree of document IDs. Return whether the tree is a valid BST: every node's key is strictly greater than all keys in its left subtree and strictly less than all keys in its right subtree.
A Google technical screen classic that catches candidates who only compare a node with its direct children. The interviewer expects either bounded recursion or an in-order check and wants you to explain why the child-only check fails.
What the Interviewer Expects
- Explain why comparing only with direct children is wrong
- Pass lower and upper bounds down the tree
- Alternatively use in-order traversal and check strictly increasing order
- Use an iterative traversal to avoid recursion limits
- Handle duplicates according to a stated rule
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if duplicates are allowed on the right side?
- Find the two nodes that violate the property if exactly two keys were swapped.
- Validate while streaming nodes in preorder without building the tree.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: binary tree root with integer keys. Output: True if it is a valid BST with strictly increasing in-order keys. Empty tree is valid. Keys may be any integer, so avoid sentinel values like `-2**31...
High-Level Design
A BST is valid exactly when its in-order traversal is strictly increasing. So I run an iterative in-order traversal with an explicit stack and keep the previously visited key; if any key is not greate...