PPractick

DSA interview question

Minimum and Maximum in a BST

Easy · Algorithms · Topics: bst

The problem

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.

Function signature

int minValue(TreeNode* root);   // non-empty BST
int maxValue(TreeNode* root);

Test cases

Input (level order)OutputExplanation
[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 5The root is the maximum
[1, null, 2, null, 3]min 1 max 3The root is the minimum
[10]min 10 max 10

(Confirm: the BST is non-empty and valid.)

How interviewers phrase it

Clarifying questions to ask

  1. Is it a BST or a general binary tree? (A BST. A general tree needs a full traversal.)
  2. Can it be empty? (Assume not, or return a sentinel.)
  3. Are duplicates allowed? (Doesn't change the answer.)

Follow-ups to expect

  1. "Why is the leftmost node the minimum?"
  2. "What if the root has no left child?"
  3. "How is this used elsewhere?"
  4. "And for a general binary tree?"
  5. "Complexity?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in