Maximize Product of Sizes of Subtrees
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon hit you with a tree problem in January 2025, and it's about subtree products. You're asked to maximize the product of sizes when you remove an edge from a tree. This is a classic tree DP trick: calculate subtree sizes bottom-up, then reason about what happens when you cut each edge. The pattern is tree traversal and dynamic programming. If you blank on the structure during the OA, StealthCoder will help you scaffold the solution without the proctor seeing a thing.
Pattern and pitfall
The core move is DFS to compute subtree sizes for every node. Once you have those, removing an edge between a parent and child splits the tree into two components. If the child's subtree has size S, the other component has size N-S. The product is S times (N-S). You iterate through all edges and track the maximum product. The trap is off-by-one errors in subtree counting or forgetting that size must be computed before you reason about products. Tree DP requires careful state management. During the live OA, if you get stuck on indexing or the recurrence, StealthCoder gives you the pattern instantly so you can type and move forward.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Maximize Product of Sizes of Subtrees cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximize Product of Sizes of Subtrees FAQ
What's the trick to avoid counting nodes wrong?+
Do a full DFS pass first and store subtree size for every node in a dictionary or array. Then do a second pass (or inline) to compute products. Don't try to calculate both simultaneously. Separation of concerns kills off-by-one bugs fast.
Does edge order matter in the answer?+
No. You're finding the maximum product across all possible edge removals. Calculate the product for every edge, keep the max, return it. Simple linear scan after DFS.
Is this harder than standard tree DP problems?+
It's medium difficulty. If you've done subtree-sum or subtree-count before, this is the same skeleton with a different operation (product instead of sum). The math is slightly messier but the algorithm is identical.
How do I handle the tree input format?+
Amazon typically gives edges as a list of pairs, or an adjacency list. Build an adjacency list first (hash map or defaultdict). Then DFS from node 0 or 1 as root. Don't assume the input is already sorted or rooted.
What if the tree is very large?+
Time complexity is O(N) for DFS plus O(N) for scanning all edges. Space is O(N) for the adjacency list and recursion stack. It scales fine. Amazon doesn't usually throw 10^6 node trees at you without hinting at it.