← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jul 2026

Summary

Amazon SWE interview with a tree problem that looks straightforward until you realize you need to track heights carefully through recursion. One question, but it had enough edge cases to keep me busy.

Questions Asked (1)

Q1

Given the root of a binary tree and an integer k, find the size (node count) of the k-th largest perfect binary subtree. A perfect binary tree has all internal nodes with two children and all leaves at the same depth. Collect the sizes of every perfect subtree in the tree into a sorted list and return the k-th largest, or -1 if fewer than k such subtrees exist.

Algorithms & Data Structures
Author's notes

My first instinct was to just do a DFS and check each subtree independently, which would have been O(n^2) and I knew that was wrong but I said it out loud anyway.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order DFS to compute the height and node count of each subtree, and determine if it is perfect by checking that both children are perfect and have equal heights. Collect the sizes of all perfect subtrees, sort them in descending order, and return the k-th largest or -1 if there are fewer than k.

Pro tip: During the DFS, you can maintain a min-heap of size k to track the k largest sizes, avoiding a full sort and achieving O(n log k) time. Also, clarify with the interviewer whether k is 1-indexed and whether the tree can be empty.

1. Clarify and Define

Confirm the definition of a perfect binary tree and the meaning of k-th largest (e.g., 1-indexed). Discuss edge cases like empty tree, k <= 0, or fewer than k perfect subtrees.

2. Design DFS Return Values

Decide that each DFS call returns a tuple: (isPerfect, height, size). For a null node, return (true, -1, 0) or similar. For a leaf, return (true, 0, 1).

3. Post-order Traversal and Perfect Check

Recursively process left and right children. A subtree is perfect if both children are perfect and their heights are equal. Compute height = left.height + 1 and size = left.size + right.size + 1.

4. Collect Sizes Efficiently

If the subtree is perfect, add its size to a min-heap of size k (or a list). If using a heap, push the size and if heap size exceeds k, pop the smallest. This keeps the k largest sizes.

5. Return the k-th Largest

After traversal, if the heap has fewer than k elements, return -1. Otherwise, the root of the min-heap is the k-th largest size. If using a list, sort descending and return the (k-1)-th element.

Key Points to Mention

  • Definition of a perfect binary tree: all internal nodes have two children and all leaves at the same depth.
  • Post-order traversal to compute subtree properties bottom-up.
  • Checking perfection by verifying both children are perfect and have equal heights.
  • Using a min-heap of size k to find the k-th largest efficiently (O(n log k) time).
  • Handling edge cases: empty tree, k <= 0, fewer than k perfect subtrees.
  • Time and space complexity analysis: O(n) time, O(h) space for recursion, plus O(k) for heap.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.