Skip to main content
All Questions

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

Medium

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

For example, given the following binary tree:

// tree 1 / \ 2 3 / \ 4 5

The diameter is 4, which is the length of the path [4, 2, 1, 3] or [5, 2, 1, 3].

Interview experiences

5 shared
Uber · 2 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
Google · 8 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
Meta · a year ago
"- everyone was super nice - questions were like leet code but with a twist - time complexity always asked"
Software Engineer · Mid Level / L4
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
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

Related courses

Course

Machine Learning Engineer Interview Prep

Land your dream machine learning role at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Explore ML system design, core concepts, coding, behavioral interviews, and more.

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.