# Binary tree diameter length — coding interview question

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/2705/binary-tree-diameter-length
Source observed: 2026-11-04T23:56:10.377Z
Last checked: 2026-11-04T23:56:10.377+00:00
Indexed: 2026-11-06T17:13:58.47+00:00

---

# Given the root of a binary tree, return the length of the diameter of the tree.

**Difficulty:** Medium  
**Category:** Data Structures & Algorithms; Coding  
**Company:** Aced (formerly Exponent)

## Problem

Given the root of a binary tree, create a function `diameterOfTree` that determines and returns the length of the tree's diameter. The diameter is the number of nodes along the longest path between any two nodes in the tree.

Example tree:

```text
        1
       / \
      2   3
     / \
    4   5
```

The diameter is **4**: either `[4, 2, 1, 3]` or `[5, 2, 1, 3]`.

The page provides tabs for **Question**, **Solution**, **Mock Interview**, and **Community**. No community answers were contributed in the supplied content. The coding workspace includes an editor, share view, output, test results, and console, with an option to [sign up to run code](https://www.aced.io/signup?src=lesson).

## Interview metadata

The rendered page shows **+210 interviews**, with the question reported as last asked **3 months ago** at the observation timestamp of 2026-11-04. The page associates the question with these roles and companies:

- Roles: [Software Engineer](https://www.aced.io/questions?role=swe) — 8; [Machine Learning Engineer](https://www.aced.io/questions?role=ml-engineer) — 2
- Companies: [Meta](https://www.aced.io/questions?company=meta-facebook) — 4; [Google](https://www.aced.io/questions?company=google) — 2; [Amazon](https://www.aced.io/questions?company=amazon) — 2; [Grammarly](https://www.aced.io/questions?company=grammarly); [Uber](https://www.aced.io/questions?company=uber)
- Categories: [Data Structures & Algorithms](https://www.aced.io/questions?type=algorithms); [Coding](https://www.aced.io/questions?type=coding)

Detailed company statistics require [Upgrade](https://www.aced.io/upgrade?src=questions).

## Interview experiences

The page lists **8 shared interview experiences**:

1. **Grammarly · 3 months ago** — “Straightforward process; recruiters were very nice and helpful; Didn't provide any feedback but I got rejected after the onsite” — Software Engineer, Senior / L5. [Read the experience](https://www.aced.io/experiences/grammarly-senior-software-engineer-interview-d9d0e6)
2. **Uber · 4 months ago** — “interview process is very lengthy took around 5 weeks, and expectations is very high in each round, for LLD working code is required, also dont mess your HM round since i felt like it contained more weightage” — Software Engineer, Mid Level / L4. [Read the experience](https://www.aced.io/experiences/uber-software-engineer-interview-db9df6)
3. **Meta · 5 months ago** — “The interview went smoothly overall. 1 round of phone screen followed by the full loop of 4 rounds packed in a day. 2 coding 1 behavior 1 system design.” — Software Engineer, Senior / L5. [Read the experience](https://www.aced.io/experiences/meta-facebook-senior-software-engineer-interview-b893a3)
4. **Google · 10 months ago** — “I interviewed for an AI Engineer internship, but the process was mostly DSA-heavy. I had an online coding round first, then a technical round with binary tree and DP questions, then another harder DSA round with DP and linked list questions, and finally an HR round. The online round covered arrays and strings, and the later rounds got progressively more difficult. The only non-DSA part was the last round, where they asked about my projects and previous experience. I didn't share the final outcome, but the main thing that stood out to me was how coding-focused the whole process was.” — Machine Learning Engineer, Intern. [Read the experience](https://www.aced.io/experiences/google-machine-learning-engineer-intern-interview-d43fed)
5. **Meta · 2 years ago** — “- everyone was super nice - questions were like leet code but with a twist - time complexity always asked” — Software Engineer, Mid Level / L4. [Read the experience](https://www.aced.io/experiences/meta-facebook-software-engineer-interview-302f4d)
6. **Meta · 2 years ago** — “The process was very nice, interviewed by people that would not be in my next team, but everybody was exceptionally nice. The questions in the ML system design round were not very deep and were more systems-oriented.” — Machine Learning Engineer, Senior / L5. [Read the experience](https://www.aced.io/experiences/meta-facebook-senior-machine-learning-engineer-interview-3dfc5c)
7. **Amazon · 2 years ago** — “All 3 rounds in loop were smooth, they are friendly and interviews went smoothly. I took entire loop in one go so it helped to be very much prepared.” — Software Engineer, Entry Level / L3. [Read the experience](https://www.aced.io/experiences/amazon-software-engineer-interview-7a0aae)
8. **Meta · 2 years ago** — “Practice leetcode fundamentals (graph, tree), practice system design fundamentals, speaking/thinking out loud, being able to explain full flow of what happens from when user/request is received to processed to returned (ensure components are disconnected/missing a flow path)” — Software Engineer, Senior / L5. [Read the experience](https://www.aced.io/experiences/meta-facebook-senior-software-engineer-interview-d7245b)

## Related questions

- [Given the root of a binary tree of integers, return the maximum path sum.](https://www.aced.io/questions/4342/binary-tree-maximum-path-sum)
- [Return root node of a binary tree given a list of shuffled nodes.](https://www.aced.io/questions/4944/binary-tree-shuffled-root-node)
- [Given a binary tree, find the shortest path from the root to a leaf.](https://www.aced.io/questions/6324/find-shortest-path-binary-tree)

## Related courses

![Machine Learning Engineer Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/6UfkHxUyLQVfDHSfLNQtWa/1be1539a65b4f6f15201f5b573c0a578/ML_Engineer_Interview_Prep_Course.jpeg?w=800&h=800&fm=webp&q=75)

### [Machine Learning Engineer Interview Prep](https://www.aced.io/courses/ml-engineer)

Prepare for machine learning roles at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other companies through mock interviews, frameworks, and advice from senior candidates. The course covers ML system design, core concepts, coding, behavioral interviews, and more.

![Software Engineering Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/4VkuYewBHUFIVUS5oJDW5h/02e833439b1289f25f38f13f81cbdca3/adobe_stock_optimized.jpeg?w=800&h=800&fm=webp&q=75)

### [Software Engineering Interview Prep](https://www.aced.io/courses/software-engineering)

Prepare for software engineering roles at Google, Amazon, Microsoft, Meta, Apple, and other companies through mock interviews, frameworks, advice from senior candidates, and practice in data structures, algorithms, system design, people management, and behavioral interviews.

## Other Aced offerings shown in the page navigation

We provide interview courses and preparation resources covering Product Management, Forward Deployed Engineering, Engineering Management, Software Engineering, System Design, Data Science, Machine Learning, Data Engineering, AI Engineering, Investment Banking, Generative AI, Security Engineer, TPM, Solutions Architect, UX / Product Design, and BizOps & Strategy. We also provide Loops, Experiences, Practice, Mock Interviews & Coaching, Resume Review, Salary Negotiation, Job Referrals, Resources, and related programs.

- [Courses](https://www.aced.io/courses?src=nav)
- [Experiences](https://www.aced.io/experiences?src=nav)
- [Questions](https://www.aced.io/questions?src=nav)
- [Practice](https://www.aced.io/practice?src=nav)
- [Coaching](https://www.aced.io/coaching?src=nav)
- [Pricing](https://www.aced.io/upgrade?src=nav)
- [Sign up](https://www.aced.io/signup?src=nav)
- [Log in](https://www.aced.io/login?src=nav)

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F2705%2Fbinary-tree-diameter-length&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F2705%2Fbinary-tree-diameter-length). Opening a form does not execute a tool.