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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

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
binary trees
post-order traversal
tree height
AVL invariant
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported