PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/Qualcomm

Compute the Diameter of an Undirected Tree

Last updated: Jul 22, 2026

Quick Overview

Compute the diameter of a large undirected tree, measured by the number of edges on its longest simple path. The exercise tests adjacency construction, linear-time tree traversal, endpoint reasoning, recursion-depth awareness, and edge cases such as a single-node tree.

  • medium
  • Qualcomm
  • Coding & Algorithms
  • Software Engineer

Compute the Diameter of an Undirected Tree

Company: Qualcomm

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Compute the Diameter of an Undirected Tree You are given an undirected tree with nodes numbered from `0` through `n - 1`. The **diameter** is the number of edges on the longest simple path between any two nodes. Return the tree's diameter. ## Function Signature ```python def tree_diameter(n: int, edges: list[list[int]]) -> int: ... ``` ## Input and Output - Each element of `edges` is `[u, v]` and represents one undirected edge. - Return the maximum number of edges on any simple path. ## Constraints - `1 <= n <= 100_000` - `len(edges) == n - 1` - `0 <= u, v < n` and `u != v` - The input graph is connected and contains no cycle. ## Examples ```text Input: n = 5, edges = [[0, 1], [1, 2], [1, 3], [3, 4]] Output: 3 ``` ```text Input: n = 1, edges = [] Output: 0 ```

Quick Answer: Compute the diameter of a large undirected tree, measured by the number of edges on its longest simple path. The exercise tests adjacency construction, linear-time tree traversal, endpoint reasoning, recursion-depth awareness, and edge cases such as a single-node tree.

Related Interview Questions

  • Find the First Bad Version in a Monotonic Release History - Qualcomm (medium)
  • Implement A* Maze Search - Qualcomm (medium)
|Home/Coding & Algorithms/Qualcomm

Compute the Diameter of an Undirected Tree

Qualcomm logo
Qualcomm
Apr 16, 2026, 12:00 AM
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
2
0

Compute the Diameter of an Undirected Tree

You are given an undirected tree with nodes numbered from 0 through n - 1. The diameter is the number of edges on the longest simple path between any two nodes.

Return the tree's diameter.

Function Signature

def tree_diameter(n: int, edges: list[list[int]]) -> int:
    ...

Input and Output

  • Each element of edges is [u, v] and represents one undirected edge.
  • Return the maximum number of edges on any simple path.

Constraints

  • 1 <= n <= 100_000
  • len(edges) == n - 1
  • 0 <= u, v < n and u != v
  • The input graph is connected and contains no cycle.

Examples

Input: n = 5, edges = [[0, 1], [1, 2], [1, 3], [3, 4]]
Output: 3
Input: n = 1, edges = []
Output: 0

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

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

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

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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.