Find the Highest Scoring Path Anywhere in a Tree

Last updated: November 29, 2026

Quick Overview

Each node in a binary tree of Azure service dependencies has an integer health score that can be negative. Return the largest sum along any path between two nodes, where a path follows parent and child edges and visits each node at most once.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Each node in a binary tree of Azure service dependencies has an integer health score that can be negative. Return the largest sum along any path between two nodes, where a path follows parent and child edges and visits each node at most once.

A well-known hard tree problem that Microsoft onsite interviewers use to test whether a candidate can separate what a recursive call returns from what it records. Negative values are the trap.

What the Interviewer Expects
  • Distinguish a path that bends at a node from one that continues to the parent
  • Return only a one-sided gain to the parent
  • Clamp negative child gains to zero
  • Track a global best that starts at negative infinity
  • Handle all-negative trees
Key Topics to Cover
binary trees
post-order DFS
tree DP
negative values
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Return the actual node sequence of the best path.
  • Restrict paths to go from a leaf to a leaf.
  • Do it without recursion.
  • What if the graph is a general tree with any number of children?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: root of a non-empty binary tree with integer values.
  • A path is any sequence of connected nodes, at least one node, no repeats.
  • Output: maximum path sum....
High-Level Design

At each node, there are two different quantities. The best path that bends here uses both children: val + left_gain + right_gain, a candidate for the global answer. But the parent can only extend a ...


Submit Your Answer
Markdown supported