AO
Back

Tree Diameter Endpoints via Double BFS (LC 1245-style)

OAAsync OALast reported June 2026Low Frequency

Problem Overview

Where you'll code it
HackerRank
Live shared coding environment. You may need to create test files / inputs yourself.
Full problem statement

Online assessment, problem 3 of 3. Citadel's 90-minute HackerRank assessment (three coding problems) ends with a tree problem. Three candidates reported it:

  • one says you are given two input arrays that you first turn into a graph, and must return, for every node, whether it is a special node: an end of the longest path in the graph
  • one says it was a BFS to find the points on the tree's longest diameter
  • one calls it the classic tree-diameter problem (LeetCode 1245)

The task as this page grades it

A tree has tree_nodes nodes numbered 1..tree_nodes and tree_nodes - 1 edges given as two arrays: edge i connects tree_from[i] and tree_to[i]. Every edge has length 1. Return a list of length tree_nodes whose k-th value is 1 if node k + 1 is special and 0 if it is not.

A node is special if it is an end of a longest path in the tree. When several paths tie for longest, a node is special if it ends any of them.

The function name, the parameter names, the numbering from 1 and the 1/0 output are ours; the reports give none of them.

Example

special_nodes(5, [1, 1, 1, 1], [2, 3, 4, 5])   ->   [0, 1, 1, 1, 1]

Node 1 is the centre of a star. Every longest path has length 2 and runs leaf, 1, leaf, so each leaf ends one and node 1 ends none.

Settle before coding (an assessment has no one to ask: pick a reading and note it in a comment)

  • Ends only, or every node on the path? The most detailed report defines a special node as an end of the longest path. Another says "the points on the longest diameter", which could mean every node along it. The two readings differ whenever the longest path has three or more nodes: in the example, node 1 lies on every longest path. This page grades the ends, the one reading a report states as a definition; the other reading is the Variant level.
  • Ties. With several longest paths of equal length, this page counts an end of any of them. No report mentions ties; "any" is the only reading that does not depend on which longest path you happen to find.
  • Input details such as numbering from 0 or 1, and whether a one-node tree can occur, are not reported.

What Reports Emphasize

What passers do: One candidate solved all three assessment problems, this one included, in about half an hour and moved on to the next round

Practice

Write your own against 9 test cases, or read the worked solution — approach, complexity, and code that runs.

Prepping for Citadel with friends?

Send them this page. It is free to read, no account needed.

More Citadel Questions

Free preview

Every question in the Citadel catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 62 Citadel questions, refreshed monthly from new candidate reports.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
Citadel · OA · Reported 3× across candidate reports
Is this helpful?