Reported November 2025
Agodabreadth first search

Special Diameter Endpoints

Reported by candidates from Agoda's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Agoda OA. Under 2s to a working solution.
Founder's read

Agoda reportedly served this one in November 2025, and the detail that matters is in Example 2: four diameter paths, and exactly four endpoints. A node is special if it ends at least one longest path in the tree. It's a tree-diameter problem with a twist, because you need every endpoint, not just one pair. With up to 10^5 nodes, brute force from every node dies. The pattern is BFS or DFS from a few chosen nodes, then a distance check. If you blank mid-assessment, StealthCoder is the invisible hedge that reads the problem and hands you a working solution.

The problem

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 3
Example 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 = 3
tree_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 = 7
tree_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^5
1 ≤ tree_from[i], tree_to[i] ≤ tree_nodes

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: run BFS from any node and take the farthest node A. Run BFS from A to get distances dA and take the farthest node B, so dA[B] is the diameter D. Run BFS from B to get dB. A node is special if max(dA[v], dB[v]) equals D. Any diameter endpoint is at distance D from some other endpoint, and the farthest-node property guarantees that A and B cover every such case. That's three linear passes, O(n). Pitfalls: recursion depth on a path-shaped tree with 10^5 nodes, so use iterative BFS. Also handle n = 1, where there are no edges and the diameter is 0. Decide up front what that single node returns, and check it against the statement. Build an adjacency list from tree_from and tree_to, and remember nodes are 1-indexed. If the distance check doesn't click live, StealthCoder can supply it as a fallback during the OA.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Special Diameter Endpoints 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Agoda's OA.

Agoda reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Special Diameter Endpoints FAQ

What's the trick for Special Diameter Endpoints?+

Use the double-BFS diameter idea. BFS from any node to find the farthest node A, BFS from A to find B and the diameter D, then BFS from B. A node is special if its distance from A or from B equals D. Three passes, linear time.

How hard is this problem really?+

Medium. If you know the standard tree diameter technique, the extension is one extra BFS and a comparison. It's hard only if you try to test every node as an endpoint, which is far too slow at 10^5 nodes.

Why not just run BFS from every node?+

That's O(n^2). With n up to 10^5 it will time out. The double-BFS approach finds all endpoints in O(n) because the two extreme nodes A and B reach every possible diameter endpoint.

What edge cases should I check?+

A single node with no edges, a straight line, and a star shape like Example 2 where many endpoints tie. Also remember nodes are numbered from 1, so size arrays n+1 or shift indices. Use iterative BFS to avoid stack overflow.

How do I prepare in 48 hours for this Agoda OA?+

Write the tree diameter solution from scratch twice using BFS and an adjacency list. Then add the third BFS and the max(dA, dB) == D check. Test on both examples from the problem. Skim other tree and graph traversal basics, since Agoda OAs reportedly lean on those.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Agoda.

OA at Agoda?
Invisible during screen share
Get it