Verify a Height-Balanced Index Tree Before Persisting a Snapshot
Last updated: November 29, 2026
Quick Overview
Before a storage service writes an index snapshot, it must confirm the binary tree is height balanced: at every node the heights of the two subtrees differ by at most one. Return whether the tree passes.
0
0
0 solved
Before a storage service writes an index snapshot, it must confirm the binary tree is height balanced: at every node the heights of the two subtrees differ by at most one. Return whether the tree passes.
A Google technical screen question that separates candidates who compute heights repeatedly (O(n^2)) from those who fold the balance check into a single post-order pass. The interviewer will usually ask for the complexity of the naive approach first.
What the Interviewer Expects
- Notice that checking only the root is not enough
- Avoid recomputing heights by returning height and status together
- Use a sentinel such as -1 to propagate failure early
- Explain why the naive solution is O(n^2) on skewed trees
- Handle the empty tree as balanced
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the first unbalanced node found in post-order.
- How would you rebalance the tree if the check fails?
- How does this relate to the AVL invariant maintained on insert?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is a binary tree root. Return True if for every node, abs(height(left) - height(right)) <= 1. The empty tree is balanced. A common mistake is only comparing the root's two subtrees: a tree can...
High-Level Design
Compute heights bottom-up in a single post-order traversal. Each call returns the subtree height, or -1 if any node below is unbalanced. Once a child reports -1 the parent returns -1 immediately, so t...