Reported June 2024
Amazondepth first search

Maximize Subtree Product

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

You're seeing this one from Amazon's June 2024 OA cycle. Maximize Subtree Product is a tree traversal problem that looks simple on the surface but has a real trick: negative numbers. You need to track both the maximum and minimum product at each node because a large negative times a negative number becomes your actual maximum. This is where most candidates slip. StealthCoder is a safety net if the recursion pattern doesn't click in the live OA.

Pattern and pitfall

The pattern here is depth-first search over a tree structure combined with dynamic programming thinking. At each node, you can either include it in a product chain or start fresh. The trap is handling negatives: you must track min and max products simultaneously because multiplying by a negative flips which is which. Common pitfall: candidates return only the max and miss cases where a negative subtree product becomes the global maximum when multiplied by another negative. The recursion needs to propagate both values up to the parent. Off-by-one thinking and forgetting to consider the node itself as a standalone product are also killers.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Maximize Subtree Product 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximum product subarray. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximize Subtree Product FAQ

Do I have to include every node in the product?+

No. You can start a fresh product at any node or skip a subtree entirely. This flexibility is why you track max at each point. The global answer is the maximum value found anywhere in your traversal, not necessarily using the root.

Why does the min matter if I only want the max?+

Because min times a negative number becomes max. If a subtree's min product is -100 and you multiply by -2, you get 200. Ignoring min means you miss valid maxima. Track both in your DFS return.

What's the time complexity I should aim for?+

O(n) where n is the number of nodes. One DFS pass, and at each node you do constant work. If you're doing nested loops or revisiting nodes, you're overcomplicating it.

Should I handle zero node values specially?+

Yes. A zero anywhere breaks a product chain. You'll need to decide: restart the product from scratch, or include the zero and reset to 0. Check the problem statement for exact constraints on values.

How do I practice this in 24 hours?+

Understand max-product subarray first (1D version). Then map it to trees: root becomes internal node, left/right children are subtree products. That mental model is the only shortcut. Coding it cleanly takes one real attempt.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it