Validate matrix valid BST
Last updated: August 31, 2025
Quick Overview
Given a binary search tree (BST) represented as a 2D matrix, write a function to validate whether the matrix correctly represents a valid BST. The function should return true if the matrix adheres to the properties of a BST, where for each node, all values in the left subtree are less than the node's value and all values in the right subtree are greater. The input will be a 2D array of integers, and the output should be a boolean indicating the validity of the BST.
Expedia
August 31, 2025138
14
3,003 solved
Given a binary search tree (BST) represented as a 2D matrix, write a function to validate whether the matrix correctly represents a valid BST. The function should return true if the matrix adheres to the properties of a BST, where for each node, all values in the left subtree are less than the node's value and all values in the right subtree are greater. The input will be a 2D array of integers, and the output should be a boolean indicating the validity of the BST.
This coding problem is frequently asked during Onsite at Expedia. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Expedia expects candidates to write production-quality code, not just solve the puzzle.
What the Interviewer Expects
- Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
- Discuss multiple approaches and trade-offs before coding
- Implement an optimal solution with clean, production-quality code
- Handle all edge cases including boundary conditions and invalid input
- Optimize both time and space complexity with clear justification
- Test your solution systematically with well-chosen examples
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
- Can you optimize the space complexity of your solution?
- What is the worst-case input for your solution?
- How would your solution change if the input was sorted?
- How would you test this solution thoroughly?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
The problem requires us to validate whether a 2D matrix correctly represents a binary search tree (BST). The properties of a BST dictate that for any given node, all values in the left subtree must be...
Approach
We will use a recursive function to validate each node's value against its valid range. The function will take three parameters: the value of the current node, the lower bound, and the upper bound. Fo...