Special Diameter Endpoints
Updated
Problem statement
Given a tree with tree_nodes nodes numbered from 1 to tree_nodes, call a node special if it is an endpoint of at least one diameter of the tree.
The diameter of a tree is the number of edges in a longest path. Return a binary array with one value per node: 1 if the node is special, otherwise 0.
2
/ \
1 3Example tree: the diameter is the path 1 – 2 – 3.
Function
isSpecial(tree_nodes: int, tree_from: int[], tree_to: int[]) → int[]Examples
Example 1
tree_nodes = 3tree_from = [2,2]tree_to = [1,3]return = [1,0,1]The tree contains edges 2-1 and 2-3. Its unique diameter is the path from node 1 through node 2 to node 3, so nodes 1 and 3 are special and node 2 is not.
Example 2
tree_nodes = 7tree_from = [1,2,3,3,1,1]tree_to = [2,3,4,5,6,7]return = [0,0,0,1,1,1,1]The four diameter paths connect one of nodes 6 and 7 to one of nodes 4 and 5. Exactly nodes 4, 5, 6, and 7 are diameter endpoints.
Constraints
1 ≤ tree_nodes ≤ 10^51 ≤ tree_from[i], tree_to[i] ≤ tree_nodes
More Agoda Online Assessment questions
- TV Series in a Production WindowOA · Seen Nov 2025
- Count One Groups by SizeOA · Seen Jul 2025
- Minimum Price With Discount CouponsOA · Seen Jul 2025
- Lexicographically Smallest Task QueueOA · Seen Jul 2025
- Highest-Ranked University by CountryOA · Seen Jun 2025
- Jump Game with Prime-3 StepsOA · Seen Jun 2025
- Team FormationOA · Seen Apr 2025
- Two-Core Process AssignmentOA · Seen Apr 2025
- Unique Digits in RangeOA · Seen Apr 2025
- Maximize Sum of Processed TimesOA · Seen Sep 2024
Similar questions at other companies
- Base Unit ConversionsAdobe · OAAdobe interview questions
- Maximum Data Transfer TimeMicrosoft · OAMicrosoft interview questions
- Longest Transfer Time in a Server TreeRippling · OARippling interview questions
- Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
- All Nodes Distance K in a Binary TreeAmazon · PHONE SCREEN
- Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
- Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
- Lowest Common Ancestor with a Changing RootInfosys · OAInfosys interview questions
- Minimum Tree Diameter After Removing K LeavesMathWorks · OAMathWorks interview questions
- Sum Dynamic Weighted Tree DistancesJuspay · OA