Shortest Path in a Weighted Tree
A hard-tier problem at 32% community acceptance, tagged with Array, Tree, Depth-First Search. Reported in interviews at Juspay and 0 others.
Shortest Path in a Weighted Tree is a hard problem that appears deceptively simple but catches candidates off guard during live assessments. You're given a tree with weighted edges and need to find shortest paths between nodes. It's asked at Juspay and tests whether you understand tree traversal beyond the basics. The 32% acceptance rate reflects a common trap: the obvious DFS or BFS approach works, but efficiency matters when the tree is large or queries are repeated. If you hit this problem live and freeze on optimization, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Shortest Path in a Weighted Tree"
Shortest Path in a Weighted 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 used it to pass JPMorgan's OA and system design loop.
Get StealthCoderThe core trick is recognizing that in a tree, there's only one path between any two nodes, so finding the shortest path is really about traversing that unique path efficiently. Candidates often start with a brute-force DFS for each query, which times out on large inputs. The real challenge emerges when you realize you need to precompute distances or use techniques like Binary Indexed Tree or Segment Tree to answer queries fast. Common pitfall: treating it like a general graph shortest-path problem and importing Dijkstra when the tree structure makes it unnecessary. Another trap is not caching node-to-node distances early. The problem rewards candidates who step back, recognize the tree constraint, and build a preprocessing step that makes queries O(log n) or better. StealthCoder becomes your hedge if you don't spot the optimization layer during the live OA.
Pattern tags
You know the problem.
Make sure you actually pass it.
Shortest Path in a Weighted 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 Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Path in a Weighted Tree interview FAQ
Is this problem really asked at interviews, or just LeetCode?+
It's reported from Juspay, a fintech company. Hard tree problems with segment trees or binary indexed trees are less common than medium-tier tree traversal, so expect it only if you're interviewing at companies that do heavy systems or data-structure work. Check the job description for signals like 'optimization' or 'large-scale trees'.
What's the trick that separates accepted from rejected solutions?+
Recognizing the tree has a unique path between any two nodes, so you're not running Dijkstra. The efficiency jump comes from precomputing all pairwise distances or using a segment tree or binary indexed tree to answer range queries in logarithmic time instead of linear.
Can I solve this with just DFS and memoization?+
DFS with memoization works for correctness but often times out on large trees or many queries. The acceptance rate is 32%, partly because naive approaches hit TLE. You need to move beyond storing individual path results and build a data structure that answers queries faster.
How does this relate to the other tree topics listed?+
Array, DFS are baseline tools. Binary Indexed Tree and Segment Tree are the optimization layer that lifts your solution from timeout to accepted. If you haven't practiced segment trees or BIT on a tree, this problem will expose that gap during the live assessment.
If I blank on this during an OA, how recoverable is it?+
Very hard to recover from scratch under time pressure if you don't know segment trees. A brute-force DFS might get partial credit but likely TLEs on full test cases. This is exactly the scenario where StealthCoder keeps you moving forward instead of burning 40 minutes on a single problem.
Want the actual problem statement? View "Shortest Path in a Weighted Tree" on LeetCode →