FastPrepFind the Tree Diameter
Problems/Find the Tree Diameter

Find the Tree Diameter

Adyen logoAdyen● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names the tree-diameter task. It leaves out how the tree is given, how length is measured, edge cases and limits. Here the tree is a connected, undirected, unweighted edge list with nodes numbered from zero. The diameter is returned as a count of edges.

Our version is about 82% the same as the reported question.

You are given an undirected tree with n vertices labeled from 0 to n - 1 and an edge list edges.

The diameter is the maximum number of edges on a simple path between any two vertices. Return the tree's diameter. A one-vertex tree has diameter 0.

Function

treeDiameter(n: int, edges: int[][]) → int

Examples

Example 1

n = 6edges = [[0,1],[1,2],[1,3],[3,4],[4,5]]return = 4

The path 2-1-3-4-5 contains four edges, and no longer simple path exists.

Example 2

n = 1edges = []return = 0

A tree with one vertex has no edges, so its diameter is zero.

Constraints

  • 1 <= n <= 200.
  • edges.length = n - 1.
  • Every edge contains two distinct labels in [0, n - 1].
  • The edges form one connected acyclic undirected graph.

More Adyen interview questions

  • Search and Format ContactsPHONE SCREEN · Seen Nov 2026
  • Count Perfect PairsOA · Seen Aug 2026
  • Maximize Strength with Disjoint Adjacent SwapsOA · Seen Aug 2026
  • Planets Queries from Planet OnePHONE SCREEN · Seen May 2026
  • Best TV Show in a GenreOA · Seen May 2026

Similar questions at other companies

  • Distance Between Two Tree NodesAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Binary Tree Nodes at Distance KApple · ONSITE INTERVIEWApple interview questions
  • Alternating-Color Binary Tree RootsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Binary Tree Nodes at Distance KGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Base Unit ConversionsAdobe · OAAdobe interview questions
  • Tree Pythagorean TriplesIBM · OAIBM interview questions
  • Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
  • Maximum Data Transfer TimeMicrosoft · OAMicrosoft interview questions
  • Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions

Adyen interview experiences

  • Adyen Kotlin Backend Engineer Live Coding: 1 Experience
All Adyen interview questionsSee Adyen hiring insights
Saved
class Solution {
    public int treeDiameter(int n, int[][] edges) {
        // Return the maximum number of edges on a simple path.
    }
}
Checking account…