PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of graph algorithms and tree properties, specifically the ability to identify and reconstruct a longest simple path (diameter) in an undirected tree.

  • medium
  • Citadel
  • Coding & Algorithms
  • Software Engineer

Return nodes on a tree diameter path

Company: Citadel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem You are given an undirected tree with `n` nodes labeled `0..n-1` (connected, no cycles). The **diameter** of a tree is the longest simple path between any two nodes. Return the nodes (in order) along **one** diameter path (if multiple diameter paths exist, returning any one of them is acceptable). ## Input - Integer `n` (`2 <= n <= 2 * 10^5`) - List of `n - 1` edges `edges`, where each edge is a pair `[u, v]` with `0 <= u, v < n` ## Output - A list/array `path` containing the node labels along a longest path (diameter), from one endpoint to the other. ## Constraints - The input graph is guaranteed to be a tree. ## Example Input: - `n = 6` - `edges = [[0,1],[1,2],[1,3],[3,4],[4,5]]` One valid output: - `[2,1,3,4,5]` Explanation: the longest path has length 4 edges, from node `2` to node `5`.

Quick Answer: This question evaluates understanding of graph algorithms and tree properties, specifically the ability to identify and reconstruct a longest simple path (diameter) in an undirected tree.

You are given an undirected tree with n nodes labeled 0 to n - 1. A tree diameter is a longest simple path between any two nodes in the tree. Return the node labels, in order, along one diameter path. If multiple diameter paths exist, returning any one valid diameter path is acceptable.

Constraints

  • 2 <= n <= 2 * 10^5
  • len(edges) == n - 1
  • 0 <= u, v < n for every edge [u, v]
  • The input graph is guaranteed to be connected and acyclic

Examples

Input: (6, [[0,1],[1,2],[1,3],[3,4],[4,5]])

Expected Output: [2,1,3,4,5]

Explanation: The path from node 2 to node 5 has 4 edges, which is a maximum-length path in this tree.

Input: (2, [[0,1]])

Expected Output: [0,1]

Explanation: With only two nodes, the single edge is the diameter path.

Input: (5, [[0,1],[1,2],[2,3],[3,4]])

Expected Output: [0,1,2,3,4]

Explanation: The tree is already a straight chain, so the entire chain is the diameter path.

Input: (5, [[0,1],[0,2],[0,3],[0,4]])

Expected Output: [3,0,4]

Explanation: Any path between two leaves has length 2, which is the diameter length. The shown path is one valid diameter.

Input: (8, [[0,1],[1,2],[2,3],[1,4],[4,5],[5,6],[6,7]])

Expected Output: [3,2,1,4,5,6,7]

Explanation: The longest path goes from node 3 to node 7 and contains 6 edges.

Hints

  1. In a tree, if you run BFS or DFS from any node, one of the farthest nodes you find is an endpoint of a diameter.
  2. After finding one diameter endpoint, run another BFS or DFS from it while storing parents so you can reconstruct the path to the farthest node.
Last updated: Jun 20, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Sort a Nearly Sorted Array - Citadel (hard)
  • Implement a single-producer multi-consumer ring buffer - Citadel (medium)
  • Compute BBO and NBBO from order data - Citadel (medium)
  • Simulate 2048 and pack board into uint64 - Citadel (medium)
  • Implement task queue with insert, delete, execute - Citadel (medium)