Longest Cable Span Problem
Longest Cable Span Problem — ExecCode Hard DSA Practice
Solve the Longest Cable Span problem on ExecCode. Free online hard DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Villages and weighted roads form a tree. The longest cable you can lay along roads is the tree diameter: the maximum distance between any two villages. Two BFS/DFS passes (farthest from an arbitrary node, then farthest from that) compute it. Rerooting DP computes the height at every root in the same family. Edges are [u, v, weight]. Return the maximum pairwise distance. You are given: n — villages edges — weighted tree edges Return the diameter length. The diameter need not pass through node 0.
Examples
Input {'n': 4, 'edges': [[0, 1, 1], [0, 2, 2], [2, 3, 3]]}; Output 6. Input {'n': 1, 'edges': []}; Output 0. Input {'n': 2, 'edges': [[0, 1, 7]]}; Output 7
Constraints
Positive weights n = 1 returns 0
Practice Longest Cable Span free on ExecCode. Browse DSA problems, topic map, and placement guides.