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

How do you find the diameter of a Binary Tree?

Difficulty: 6/10
tree traversal, depth-first search, recursive algorithms

Binary Tree Diameter

The diameter is the longest path between any two nodes in the tree. A key observation is that for each node, a candidate diameter passes through that node and has length equal to the height of its left subtree plus the height of its right subtree. A postorder traversal computes subtree heights bottom-up while maintaining the maximum diameter.

javascript
  1. 1

    Use postorder traversal.

  2. 2

    Height is computed once for every node.

  3. 3

    Time complexity: O(n).

  4. 4

    Auxiliary recursion space: O(h).

  5. 5

    If diameter is defined in nodes rather than edges, add one to the edge-based result for a non-empty tree.

Scenario Questions

0-2 years experience

  1. 1We have a small binary tree representing a company's org chart with 7 nodes. How would you write a function to compute its diameter?
  2. 2If you accidentally swapped left and right child pointers in your recursion, what would happen to the diameter calculation?

2-5 years experience

  1. 1Your team is adding a feature that needs the longest path between any two nodes in a binary tree stored in a database. How would you implement the diameter calculation, and what trade‑offs would you consider regarding recursion vs. iterative approaches?
  2. 2During a code review you notice the diameter function runs in O(N^2) for certain inputs. How would you debug and improve its performance?

5-8 years experience

  1. 1Our service processes millions of tree structures per second and needs the diameter for each. How would you redesign the algorithm to handle this scale, considering memory usage and parallelism?
  2. 2If the tree can be modified concurrently by other threads while you compute its diameter, what synchronization or lock‑free strategies would you employ?

8+ years experience

  1. 1We are migrating a legacy system that stores hierarchical data in a relational DB to a graph service. The new service must expose an API to get the diameter of any subtree. How would you design the overall architecture to compute diameters efficiently while supporting frequent updates?
  2. 2Across multiple teams, the definition of 'diameter' has diverged (some count edges, others count nodes). How would you lead a cross‑team effort to standardize the metric and refactor existing codebases without breaking downstream services?

Follow-up Questions

  • How would you adapt the solution to also return the actual longest path?
  • What is the space complexity, and can it be reduced further?
  • How does the algorithm change if the tree is stored as parent pointers instead of child pointers?
Share

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