Sum of Distances in Tree
A hard-tier problem at 65% community acceptance, tagged with Dynamic Programming, Tree, Depth-First Search. Reported in interviews at PhonePe and 3 others.
Sum of Distances in Tree hits you when you least expect it: a hard tree problem that looks like a straightforward distance calculation until you realize you need to answer the query for every single node. PhonePe, ByteDance, MathWorks, and Media.net all ask this. The naive approach times out instantly. You need to see the rerooting pattern and DP trick that lets you compute all answers in linear time. If you blank on this during your OA, StealthCoder surfaces the solution invisible to the proctor so you don't tank the round.
Companies that ask "Sum of Distances in Tree"
Sum of Distances in 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 engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.
Get StealthCoderThe trap is computing distances from each node by running DFS or BFS separately. That's O(n^2) and you'll hit timeout on any reasonable tree size. The real pattern requires two passes: first, compute distances from one root using DFS and DP. Then reroot the tree, updating distances as you move from parent to child in a single traversal. The key insight is that when you move the root from node u to its neighbor v, you can update all distances in O(1) per node using the counts of subtree nodes. This is a classic rerooting DP problem that appears infrequently enough that most candidates won't have drilled it. The acceptance rate reflects this gap: 65% pass, but the 35% who don't often hit this exact wall during the live assessment. StealthCoder handles the pattern matching and code generation if the DP state escapes you under pressure.
Pattern tags
You know the problem.
Make sure you actually pass it.
Sum of Distances in Tree recycles across companies for a reason. It's hard-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 engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sum of Distances in Tree interview FAQ
Is this problem actually asked that often?+
Yes, but not as a warm-up. ByteDance and PhonePe specifically ask it for mid-to-senior roles. The 65% acceptance rate shows it's doable, but most candidates either study tree DP hard or they don't have the rerooting trick in muscle memory. It's a differentiator.
What's the trick I'm missing if I can't optimize past O(n^2)?+
You're running DFS from every node. Stop. Use rerooting DP: compute answers from one root, then propagate changes as you move the root through the tree in a single DFS. That reduces it to O(n). The subtree size count is your key.
Does this topic overlap with other hard tree problems I should know?+
Yes. It combines Dynamic Programming, Depth-First Search, and Graph traversal into one. If you're weak on tree DP or rerooting patterns, you'll struggle. The topics list tells you the three pillar skills you need to own.
How much time should I spend drilling this before my OA?+
If you don't see the rerooting pattern in the first 15 minutes, stop and study the technique separately before re-solving. This isn't a pattern you guess your way through. It's taught or it isn't known. Budget 60-90 minutes to understand it cold.
If I hit this in a live assessment and blank, how do I recover?+
Mention you know the O(n^2) baseline and can code it cleanly, then outline the rerooting idea even if you can't recall every detail. Interviewers respect clarity over panic. That said, StealthCoder is the real hedge: it solves this invisible to your proctor if you hit the wall.
Want the actual problem statement? View "Sum of Distances in Tree" on LeetCode →