Nodes on a Tree's Longest Path
Updated
Problem statement
You are given a tree with n nodes numbered from 0 to n - 1. The list edges describes its edges: edges[i] = [a, b] means there is an undirected edge between nodes a and b. Every edge has length 1, so the length of a path is its number of edges. A longest path in the tree is called a diameter. A single node is a path of length 0.
Return the nodes of a diameter in order, from one end of the path to the other.
A tree can have more than one diameter, and each one can be read from either end. Return the one whose first node is as small as possible. If several diameters start at that node, return the one whose last node is as small as possible. In a tree there is exactly one path between any two nodes, so this rule picks one answer.
Aim for O(n) time. Breadth-first search is all you need.
Function
treeDiameterPath(n: int, edges: int[][]) → int[]Examples
Example 1
n = 7edges = [[0,1],[1,2],[1,3],[3,4],[4,5],[3,6]]return = [0,1,3,4,5]The longest paths have 4 edges: 0 - 1 - 3 - 4 - 5 and 2 - 1 - 3 - 4 - 5. Each can also be read starting from node 5. The smallest node that starts a longest path is 0, and the only longest path from 0 ends at 5.
Example 2
n = 8edges = [[0,1],[0,2],[2,3],[2,4],[1,5],[1,6],[0,7]]return = [3,2,0,1,5]Every longest path has 4 edges. It starts at node 3 or 4, passes through nodes 2, 0 and 1, and ends at node 5 or 6, or the same read backwards. The smallest possible first node is 3. From 3, the paths to 5 and 6 both have 4 edges, and 5 is smaller.
Example 3
n = 2edges = [[1,0]]return = [0,1]The tree has one edge, so the only longest path joins 0 and 1. Read from the smaller end, it is [0, 1].
Constraints
1 <= n <= 2 * 10^4edges.length == n - 1edges[i].length == 20 <= edges[i][0], edges[i][1] < n- The edges form a tree: there is exactly one path between any two nodes.
More Citadel Online Assessment questions
- Speed Up a Slow FunctionOA · PHONE SCREEN · Seen Nov 2026
- Possible MEX Values After One Optional IncrementOA · Seen Oct 2026
- Busy IntersectionOA · Seen Sep 2026
- Shortest Path in an Unweighted GraphOA · Seen Sep 2026
- Maximize Workday EarningsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Changes for Palindromic BlocksOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Image Processing CostOA · Seen Jul 2026
- Coprime Pick with Change CostOA · Seen Jul 2026
- Process SchedulingOA · Seen Mar 2026
- Employee With the Most OverlapsOA · Seen Feb 2026
Similar questions at other companies
- Base Unit ConversionsAdobe · OAAdobe interview questions
- Tree Pythagorean TriplesIBM · OAIBM interview questions
- Maximum Data Transfer TimeMicrosoft · OAMicrosoft interview questions
- Longest Transfer Time in a Server TreeRippling · OARippling interview questions
- Lowest Common Ancestor with a Changing RootInfosys · OAInfosys interview questions
- Minimum Tree Diameter After Removing K LeavesMathWorks · OAMathWorks interview questions
- Special Diameter EndpointsAgoda · OAAgoda interview questions
- Thread Count (Also for Infra Automation Intern)Snowflake · OASnowflake interview questions
- Distance Between Two Tree NodesAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
Citadel interview experiences
- Citadel Securities Quant Researcher Intern Loop: 1 Experience
- Citadel Securities Research Engineer Interview: 1 Experience
- Citadel Securities SWE Intern First Round: 2 Experiences
- Citadel Securities Experienced SWE Interviews: 1 Experience
- Citadel SWE Intern Later Rounds and Superday: 13 Experiences
- Citadel SWE Intern First-Round Questions: 20 Experiences