← eBay Interview Insights

eBay·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

eBay software engineer interview with a tree problem that looked clean on the surface but had enough moving parts to trip you up if you weren't careful about what you were tracking at each node.

Questions Asked (1)

Q1

Given a binary tree, count how many subtrees satisfy two conditions simultaneously: the absolute difference between the left and right subtree heights is at most 1, and the sum of all node values in that subtree is even.

Algorithms & Data Structures
Author's notes

The two-condition part is what got me at first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order DFS that returns both the height and the sum of node values for each subtree. At each node, check if the height difference between left and right subtrees is ≤1 and if the total sum is even; if so, increment a counter. This single traversal achieves O(n) time and O(h) space.

Pro tip: Clarify that 'subtree' means any node and all its descendants, not just the entire tree. Also, mention that the height of a null subtree is typically -1 (or 0) and confirm the convention with the interviewer to avoid off-by-one errors.

1. Clarify definitions and constraints

Confirm what constitutes a subtree (any node and its descendants), how height is defined (edges vs. nodes, null height), and whether node values can be negative. This ensures alignment with the interviewer.

2. Design recursive function

Define a helper that returns a pair (height, sum) for a given node. For null, return (-1, 0) or (0, 0) based on agreed convention. Recursively compute left and right results.

3. Check conditions and count

At each node, compute height difference and total sum. If |leftHeight - rightHeight| ≤ 1 and sum is even, increment a global counter. Return the node's height and sum to the parent.

4. Analyze complexity and edge cases

State that the algorithm visits each node once, giving O(n) time and O(h) space for recursion stack. Discuss edge cases: empty tree, single node, skewed tree, negative values affecting parity.

5. Test with examples

Walk through a small example (e.g., a balanced tree with even sums) to verify the logic. Optionally, mention iterative post-order traversal to avoid recursion limits.

Key Points to Mention

  • Post-order traversal to compute subtree properties bottom-up.
  • Returning multiple values (height and sum) from each recursive call.
  • Height difference condition: |leftHeight - rightHeight| ≤ 1.
  • Sum parity check: sum % 2 == 0.
  • Time complexity O(n) and space complexity O(h).
  • Handling null nodes and defining height consistently.

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