Binary Tree Maximum Path Sum

Asked atQualcommDoorDashMicrosoftNutanixOracle
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Define DFS(node) as the best downward path gain that can be extended by the parent.
  2. 02Clamp each child gain at zero because a negative branch should be excluded.
  3. 03Use both child gains to update the best complete path passing through the current node.
  4. 04Return the node value plus only the better child gain to preserve a single path.

Reference answer

Then expect these follow-ups

  • How would you also reconstruct the nodes on the maximum path?

    Tests: state reconstruction

  • Why can the value returned to the parent include only one child?

    Tests: correctness reasoning

Hiring now

Qualcomm has 707 open roles right now

Also hiring: DoorDash, Nutanix

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • CAD Engineer – AMS & Custom Layout

    Toronto, Ontario, Canada

  • GPU Design Verification Engineer

    Santa Clara, California, United States of America

  • Senior Product & Test Engineer, RF & Mixed-Signal Semiconductors

    Singapore, Central Singapore, Singapore

Or practice this question first

Next question