Questions
8 of 26
1What is a Tree? Define Node, Root, Leaf, Edge, Height, Depth.
2What is a Binary Tree?
3What is a Binary Search Tree (BST)? What are its properties?
4What is the difference between a Binary Tree and a Binary Search Tree?
5Explain Tree Traversals: Inorder, Preorder, Postorder, Level Order.
6How do you implement Inorder traversal iteratively? (Without recursion)
7How do you find the Lowest Common Ancestor (LCA) of two nodes in a BST?
8How do you validate if a given Tree is a valid BST?
9What is a Balanced Tree? Why do we need balance?
10What is a Complete Binary Tree? A Full Binary Tree? A Perfect Binary Tree?
11What is a Heap? (Min-Heap and Max-Heap)
12How is a Heap implemented using an Array? (Parent/Child index math)
13What is the time complexity of insertion and deletion in a Heap? Why?
14What is Heapify? Explain the process.
15What is a Trie (Prefix Tree)? When is it used?
16How does a Trie compare to a Hash Table for string storage?
17What is an AVL Tree? What is a Red-Black Tree? What is the difference?
18Why do databases prefer B-Trees/B+ Trees over Binary Search Trees?
19How do you serialize and deserialize a Binary Tree?
20What is a Segment Tree? What problems does it solve?
21What is a Fenwick Tree (Binary Indexed Tree)?
22What is the difference between a B-Tree and a B+ Tree? Why are B+ Trees better for disk access?
23Explain the concept of Tree Rotation.
24What is a Splay Tree? When would you use it?
25How do you find the diameter of a Binary Tree?
26How do you check if a tree is symmetric (Mirror image)?
08 / 26

How do you validate if a given Tree is a valid BST?

Difficulty: 5/10
binary search tree, tree traversal, recursion

BST Validation

The robust approach is to carry a valid numeric range down the tree. Each node must fall strictly between its inherited lower and upper bounds under the standard no-duplicates BST definition. Checking only the immediate left and right child is insufficient because BST violations can occur deeper in a subtree.

javascript
  1. 1

    Carry lower and upper bounds to every subtree.

  2. 2

    Left subtree updates the upper bound.

  3. 3

    Right subtree updates the lower bound.

  4. 4

    Time complexity: O(n).

  5. 5

    Space complexity: O(h) due to recursion.

  6. 6

    The duplicate policy must be explicitly defined.

Scenario Questions

0-2 years experience

  1. 1Given a simple binary tree node class, how would you write a function to check if the tree is a valid BST? Walk me through your approach.
  2. 2If the tree contains duplicate values, how would you decide whether they should be considered valid in a BST validation?
  3. 3What would happen if you performed an in‑order traversal but forgot to handle the case where the left subtree is empty?

2-5 years experience

  1. 1Our service receives a serialized tree from a client and we need to verify it's a BST before processing. The current recursive min/max check sometimes crashes on deep trees. How would you debug and improve it?
  2. 2We switched from a recursive to an iterative in‑order traversal for BST validation to avoid stack overflow. What trade‑offs should we consider regarding time, space, and code complexity?
  3. 3During a recent release, a tree with values [5,5] was incorrectly marked as valid. Explain why the existing validation fails and how you'd fix it.

5-8 years experience

  1. 1Our database stores millions of BSTs and runs a nightly job to validate each one. Design an approach that can validate trees in parallel while keeping memory usage low.
  2. 2We need to add BST validation to a distributed microservice that receives partial tree fragments from different nodes. How would you ensure the overall structure is a valid BST without sending the entire tree over the network?
  3. 3Explain how you would modify the validation algorithm to also check balance factors for a self‑balancing BST like an AVL tree efficiently.

8+ years experience

  1. 1Our legacy codebase has many custom tree implementations across services. We want to standardize BST validation organization‑wide. What architectural strategy would you propose to enforce a consistent, testable validation contract while minimizing impact on existing services?
  2. 2We are migrating from an on‑prem monolith to a cloud‑native platform, moving tree‑based indexes to a distributed store. How would you design a validation pipeline that can verify BST properties at scale and integrate with CI/CD?
  3. 3Multiple teams need to extend the tree node with extra metadata (timestamps, permissions). How would you design the validation API to stay robust and type‑safe across languages and future extensions?

Follow-up Questions

  • What edge cases would you test for this function?
  • How would you adapt your solution for a tree that can contain duplicate keys?
  • Can you discuss the space complexity trade‑offs between recursive and iterative approaches?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.