DSA interview question
A pricing service keeps product prices in a BST and constantly needs the cheapest and most expensive price. In a BST, smaller keys are always to the left, so the minimum is the leftmost node: keep going left until there's no left child. Symmetrically, the maximum is the rightmost node. Each walk follows one path, so it costs O(h), and no comparisons with other nodes are needed.
Example: BST [8, 3, 10, 1, 6, null, 14, null, null, 4, 7, 13] → min 1, max 14.
int minValue(TreeNode* root); // non-empty BST
int maxValue(TreeNode* root);
| Input (level order) | Output | Explanation |
|---|---|---|
[8, 3, 10, 1, 6, null, 14, null, null, 4, 7, 13] | min 1 max 14 | |
[5, 3, 8, 2, 4, 7, 9] | min 2 max 9 | |
[5, 4, null, 3] | min 3 max 5 | The root is the maximum |
[1, null, 2, null, 3] | min 1 max 3 | The root is the minimum |
[10] | min 10 max 10 |
(Confirm: the BST is non-empty and valid.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in