Sum Dynamic Weighted Tree Distances
Reported by candidates from Juspay's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Juspay's September 2026 OA has a tree problem with a detail that makes brute force useless: n and the query count both go up to 2 * 10^5, and edge weights change between distance queries. You get weighted edges, type 1 updates that replace a weight, and type 2 queries that ask for the path distance. You return the sum of all type 2 answers. It's a tree with point updates and path sums. If the approach doesn't come to you, StealthCoder is the safety net that runs invisibly during the live assessment and gives you a working solution.
The problem
You are given a connected, undirected tree with n vertices numbered from 1 to n. Each row [u, v, weight] in edges describes one weighted edge. Process every row in queries from left to right: [1, u, v, newWeight] replaces the current weight of the existing undirected edge between u and v with newWeight. [2, u, v] asks for the current shortest-path distance between u and v. Because the graph is a tree, this is the sum of the current edge weights on their unique path. Return the sum of the answers to all type 2 queries. If there are no type 2 queries, return 0. Function sumTreeQueryDistances(n: int, edges: int[][], queries: int[][]) → long Examples Example 1 n = 5 edges = [[1,2,4],[1,3,2],[3,4,7],[3,5,1]] queries = [[2,2,4],[1,1,3,5],[2,2,4],[2,4,5]] return = 37 The first distance is 4 + 2 + 7 = 13. Replacing the weight of edge (1, 3) by 5 makes the next distance 4 + 5 + 7 = 16. The last distance, from 4 to 5, is 7 + 1 = 8. Their sum is 13 + 16 + 8 = 37. Example 2 n = 3 edges = [[1,2,1000000000],[2,3,1000000000]] queries = [[2,1,3],[1,2,1,3],[2,1,3],[2,2,2]] return = 3000000003 The first path has length 2,000,000,000. The update names edge (1, 2) in reverse order and replaces its weight by 3, so the next path has length 1,000,000,003. The distance from vertex 2 to itself is 0. The total is 3,000,000,003. Example 3 n = 1 edges = [] queries = [[2,1,1]] return = 0 A path from the only vertex to itself contains no edges, so its distance and the returned sum are both 0. Constraints 1 <= n <= 2 * 10^5. edges.length = n - 1. Every row in edges is [u, v, weight], where 1 <= u, v <= n and 1 <= weight <= 10^9. edges forms a connected, undirected tree. 1 <= queries.length <= 2 * 10^5. Every query is either [1, u, v, newWeight] or [2, u, v]. Every type 1 query names an existing tree edge and satisfies 1 <= newWeight <= 10^9. Every type 2 query satisfies 1 <= u, v <= n. The returned sum fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to stop thinking about paths and think about root distances. Root the tree at vertex 1 and run an iterative DFS to get tin/tout times, depth, and LCA structure. Then dist(u,v) = D(u) + D(v) - 2*D(lca), where D(x) is the weighted distance from the root. A weight update on edge (p,c), with c as the child, changes D for every node in c's subtree by the delta. That's a contiguous Euler range, so use a Fenwick tree with range add and point query. LCA comes from binary lifting, which is static since the shape never changes. Pitfalls: recursion depth at 2 * 10^5 will blow the stack, so go iterative. Updates can name the edge as (v,u) in either order, so decide the child by depth. Use 64-bit sums, since Example 2 already passes 3 billion. If you blank on the live OA, StealthCoder is the hedge.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Sum Dynamic Weighted Tree Distances 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Juspay's OA.
Juspay reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sum Dynamic Weighted Tree Distances FAQ
What's the core trick for the Juspay tree distance problem?+
Express distance as D(u) + D(v) - 2*D(lca), where D is the weighted distance from the root. An edge update then becomes a range add over the child's Euler-tour subtree, and each query becomes a few point reads plus one LCA lookup.
Why does brute force fail here?+
Walking the path per query costs O(n) on a skewed tree. With 2 * 10^5 queries and 2 * 10^5 vertices that's around 4 * 10^10 steps. You need roughly O(log n) per update and per query, which the Fenwick plus LCA setup gives you.
Which data structures do I need?+
An adjacency list, an iterative DFS for tin, tout, depth and parent, a binary lifting table for LCA, and a Fenwick tree supporting range add and point query. Store the initial root distances in an array and add the Fenwick value on top.
What edge cases break most solutions?+
Updates given as (v,u) instead of (u,v), queries where u equals v, and n equal to 1 with no edges. Also overflow: sums exceed 32-bit quickly, so use long everywhere, including the delta newWeight minus oldWeight.
How do I prepare for this in 48 hours?+
Write one clean template: iterative Euler tour, binary lifting LCA, and a range-update Fenwick. Test it on the three examples, especially the reversed-edge one. Time yourself coding it from scratch. Tree plus updates problems reuse these same pieces.