Questions
7 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)?
07 / 26

How do you find the Lowest Common Ancestor (LCA) of two nodes in a BST?

Difficulty: 5/10
binary search tree, lowest common ancestor, tree traversal

LCA in BST

The BST ordering property makes LCA straightforward. Starting at the root, if both target values are smaller, move left. If both are larger, move right. Otherwise, the current node is the split point and therefore the lowest common ancestor, assuming both target nodes exist in the tree.

javascript
  1. 1

    If both values are less than the current node, go left.

  2. 2

    If both values are greater, go right.

  3. 3

    Otherwise the current node is the LCA.

  4. 4

    Time complexity: O(h).

  5. 5

    Iterative implementation uses O(1) auxiliary space.

Scenario Questions

0-2 years experience

  1. 1Given a BST and two node values, how would you write a function to return their lowest common ancestor?
  2. 2If the BST is unbalanced and one of the nodes is the root, what will your LCA algorithm return?
  3. 3How would you modify the algorithm to return null when either of the target nodes does not exist in the tree?

2-5 years experience

  1. 1Our service stores user permissions in a BST and we need to find the deepest shared permission node for two users; how would you adapt the LCA algorithm, and what edge cases would you watch for?
  2. 2During a recent deployment, the LCA function started returning null for some node pairs; how would you debug it given the BST may contain duplicate values?
  3. 3If the tree is being modified concurrently, what considerations would you add to ensure the LCA computation remains correct?

5-8 years experience

  1. 1We plan to cache LCA results for frequent queries in a large‑scale recommendation engine; discuss the tradeoffs of caching versus recomputing on each request.
  2. 2When the BST is stored on disk and traversed via I/O, how would you redesign the LCA algorithm to minimize disk seeks?
  3. 3If the BST grows to billions of nodes and we need sub‑millisecond LCA latency, what indexing or auxiliary data structures would you consider?

8+ years experience

  1. 1Our legacy monolith uses an in‑memory BST for hierarchical data, but we are moving to a microservices architecture with a distributed graph store; how would you migrate LCA functionality while preserving API contracts?
  2. 2Across teams, some services use AVL trees while others use Red‑Black trees; how would you create a unified LCA service that abstracts away the underlying tree implementation?
  3. 3What long‑term maintenance concerns arise from exposing LCA as a public API, and how would you version it to handle future changes in tree structure?

Follow-up Questions

  • What is the time and space complexity of your solution?
  • How does your approach handle cases where one node is not present in the tree?
  • Can you walk me through how you would test this function?
Share

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