PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Citadel

Return nodes on a tree diameter path

Last updated: Mar 29, 2026

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.

Related Interview Questions

  • Implement a single-producer multi-consumer ring buffer - Citadel (medium)
  • Sort a Nearly Sorted Array - Citadel (hard)
  • Compute BBO and NBBO from order data - Citadel (medium)
  • Design dynamic weighted random sampling with updates - Citadel (medium)
  • Compute maximum later-earlier difference - Citadel (medium)
Citadel logo
Citadel
Jan 9, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
5
0
Loading...

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.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Citadel•More Software Engineer•Citadel Software Engineer•Citadel Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ 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.