← Citadel Interview Insights

Citadel·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Citadel coding round, one algorithmic problem on tree diameter. Pretty focused session, no fluff.

Questions Asked (1)

Q1

Given an undirected tree, find the longest path (diameter) and return the actual list of nodes along that path.

Algorithms & Data Structures
Author's notes

The two-BFS trick is something I'd seen before but blanked on the second pass.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use two BFS/DFS traversals: first from any node to find one endpoint of the diameter, then from that endpoint to find the other endpoint and reconstruct the path using parent pointers. This approach runs in O(n) time and O(n) space, which is optimal for a tree.

Pro tip: Mention that the two-pass BFS/DFS method works because trees are acyclic and connected, and that you can reconstruct the path by storing parents during the second traversal. Also, note that if the tree is very large, an iterative BFS avoids recursion depth issues.

1. Clarify and confirm assumptions

Confirm that the tree is undirected, connected, and acyclic, and that nodes are uniquely identifiable. Ask if the tree is given as an adjacency list or edges, and whether recursion depth is a concern.

2. Find one endpoint of the diameter

Run BFS or DFS from any arbitrary node (e.g., node 0) to find the farthest node A. This node is guaranteed to be an endpoint of a diameter.

3. Find the other endpoint and reconstruct path

Run BFS or DFS from node A, tracking parent pointers, to find the farthest node B. The path from A to B is the diameter; reconstruct it by following parents from B back to A.

4. Analyze complexity and edge cases

State that time complexity is O(n) and space is O(n). Discuss edge cases: single node (diameter length 0), two nodes, and a star graph where the diameter is between two leaves.

5. Test with examples

Walk through a small example (e.g., a tree with 5 nodes) to verify the algorithm and path reconstruction. Mention that you would test with a line graph, balanced tree, and star graph.

Key Points to Mention

  • Two BFS/DFS passes: first to find one endpoint, second to find the other and reconstruct the path.
  • Proof of correctness: in a tree, the farthest node from any node is an endpoint of a diameter.
  • Use of parent pointers (or predecessor array) to reconstruct the actual path.
  • Time and space complexity: O(n) time, O(n) space, where n is the number of nodes.
  • Handling edge cases: single node, two nodes, and trees with multiple diameters (any valid path is acceptable).
  • Iterative BFS/DFS to avoid stack overflow for deep trees.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.