Check Whether a OneDrive Folder Tree Is Height Balanced
Last updated: November 29, 2026
Quick Overview
Model a OneDrive folder hierarchy as a binary tree. Return True if, for every folder, the heights of its left and right subtrees differ by at most one.
Microsoft
November 29, 20260
0
0 solved
Model a OneDrive folder hierarchy as a binary tree. Return True if, for every folder, the heights of its left and right subtrees differ by at most one.
A short tree question common in Microsoft phone screens. The interviewer checks recursion fluency and whether the candidate avoids the O(n^2) version that recomputes heights at every node.
What the Interviewer Expects
- Define height of an empty tree as 0
- Check the balance condition at every node, not only the root
- Compute height and balance in one post-order pass
- Short-circuit with a sentinel once imbalance is found
- Mention recursion depth on skewed trees
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
- Make it iterative to avoid stack overflow on a degenerate tree.
- Return the deepest unbalanced node.
- How would you rebalance the tree once you know it is unbalanced?
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: root of a binary tree (possibly None).
- Balanced means every node's left and right subtree heights differ by at most 1.
- Output: boolean....
High-Level Design
The naive approach calls height() on both children at every node, which costs O(n log n) on balanced trees and O(n^2) on skewed ones. Instead, one post-order pass returns the height of each subtree,...