Code Room
CodingMediumcod-g556
Subject Balanced treesLevel Mid–Senior~30 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given a binary tree encoded as a level-order array (None marks a missing child), determine the largest number of nodes contained in any subtree that is height-balanced in the AVL sense, where 'height-balanced' means that for every node in that subtree the heights of its left and right children differ by at most 1. Compute it bottom-up in a single post-order pass: each node reports its height, whether its subtree is balanced, and its node count, so a parent can combine children in O(1). Return the size of the largest balanced subtree.

Implement
largest_balanced_subtree(level: list) → int
Examples
in[[1,2,2,3,3,null,null,4,4]]out5
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.