Choose Edges to Maximize Score in a Tree
A medium-tier problem at 56% community acceptance, tagged with Dynamic Programming, Tree, Depth-First Search. Reported in interviews at Sprinklr and 0 others.
You're given a tree where each node has a value, and you need to choose edges to delete such that the resulting forest maximizes your score. This is a medium-difficulty problem that mixes tree traversal with optimization, and it's asked at Sprinklr. The twist: deleting an edge splits the tree, and your score is the sum of squares of subtree values. The naive greedy approach fails because you can't see the full impact of each deletion without recomputing. Tree DP is the lever. If you hit this live and don't see the recurrence immediately, StealthCoder surfaces a working solution invisible to the proctor.
Companies that ask "Choose Edges to Maximize Score in a Tree"
Choose Edges to Maximize Score in a Tree is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who realized the OA tests how well you memorized 200 problems, not how well you code.
Get StealthCoderThe core insight is that you must decide for every edge whether to keep or delete it, and each decision affects the final score. A brute-force try-all-subsets approach is exponential. Instead, DFS with dynamic programming computes two states per node: the maximum score in the subtree if you keep the edge to its parent, and the maximum score if you delete it. When you delete an edge, that subtree becomes its own connected component, and its contribution to the total score locks in as the square of its sum. The trap most candidates hit is conflating 'node value' with 'subtree value', or trying to use greedy heuristics that look locally optimal but miss globally better cuts. Common pitfall: forgetting that once you delete an edge, you can't re-add it, so the order of decisions matters only in how they're counted, not in sequence. Walk through the recurrence carefully before coding.
Pattern tags
You know the problem.
Make sure you actually pass it.
Choose Edges to Maximize Score in a Tree recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who realized the OA tests how well you memorized 200 problems, not how well you code. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Choose Edges to Maximize Score in a Tree interview FAQ
How hard is this problem really for a medium?+
The acceptance rate sits at 56%, which is fair for a medium. The DP pattern itself is standard tree DP, but the state definition and how to combine subtree results trips people up. If you've drilled tree DP on similar problems (like Rearrange Sticks for Fence), you'll recognize the shape faster.
Is this still asked at Sprinklr in recent rounds?+
It's reported from Sprinklr. Companies occasionally cycle problems, but tree optimization questions remain evergreen in SDE interviews. If it's on your prep list, assume it's live or will appear in future batches.
What's the trick to not overthinking this?+
Stop trying to find a greedy heuristic. Write out the recurrence first: for each node, compute two values (keep or delete edge to parent). Rewrite those states as 'max score if subtree is connected to parent' vs 'max score if subtree is isolated'. The isolation case squares the subtree sum and adds it directly to the result.
How does this relate to tree DP and DFS?+
DFS visits every node; tree DP caches decisions at each node to avoid recomputation. Here, you use DFS to traverse and at each node you combine the DP values from children to build the parent's states. It's the canonical tree DP structure: recurse, collect child results, decide, return.
What's the most common implementation mistake?+
Forgetting to include the root node's isolated value in the final answer. If the root is never 'deleted' from its parent, some candidates drop that contribution. Also, index errors when iterating children or off-by-one errors in accumulating sums. Trace through a small example with 3 nodes before submitting.
Want the actual problem statement? View "Choose Edges to Maximize Score in a Tree" on LeetCode →