Diameter of an Acyclic Undirected Graph
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The adjacency list is what this Salesforce OA, reported in August 2026, hinges on. You get n and an edge list, and you have to turn that into something you can traverse fast. The task is the diameter of a tree: the most edges on any simple path. It's a graph problem wearing a tree costume, and n goes up to 100000, so the shape of your traversal matters. If you blank on the trick, StealthCoder is the invisible hedge running on your screen during the live OA. But the trick here is short enough to own tonight.
The problem
You are given a connected, undirected, acyclic graph, so the graph is a tree. The tree has n vertices labeled from 0 to n - 1 and n - 1 edges. The diameter of the tree is the maximum number of edges on any simple path between two vertices. Implement treeDiameter(n, edges) and return the tree's diameter. edges[i] = [u, v] denotes an undirected edge between vertices u and v. A tree with one vertex has diameter 0. Function treeDiameter(n: int, edges: int[][]) → int Examples Example 1 n = 4 edges = [[0,1],[1,2],[1,3]] return = 2 A longest path is 2 - 1 - 3, which contains two edges. Example 2 n = 1 edges = [] return = 0 The single-vertex tree has no edges. Constraints 1 <= n <= 100000 edges.length = n - 1 0 <= u, v < n The input graph is connected and acyclic.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build an adjacency list, then run two BFS or DFS passes. Start from any node, say 0, and find the farthest node A. Run again from A and find the farthest node B. The distance from A to B is the diameter. It works because in a tree, the farthest node from any start is always an endpoint of some longest path. The alternative is one DFS that tracks the two deepest child heights at each node and updates a global max. Pitfalls: recursive DFS can blow the stack at 100000 nodes in a chain, so go iterative with BFS or an explicit stack. Also handle n = 1 with empty edges and return 0 before anything else. Count edges, not nodes. If the live OA freezes you, StealthCoder can supply the two-BFS solution as a safety net.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Diameter of an Acyclic Undirected Graph cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Salesforce's OA.
Salesforce reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Diameter of an Acyclic Undirected Graph FAQ
What's the trick to Diameter of a Tree?+
Two traversals. BFS from any node to find the farthest node A, then BFS from A to find the farthest node B. The distance to B is the diameter. It's valid only because the graph is a tree, with no cycles and exactly one path between any two nodes.
How hard is this Salesforce OA question really?+
Medium at most. The code is short once you know the two-pass idea. The difficulty is seeing it. If you've done tree depth problems, the one-DFS version with top two child heights is the same muscle. Setup and edge cases are where people slip.
Should I use recursion or iteration?+
Iteration is safer. With n up to 100000, a path-shaped tree gives recursion depth of 100000, which can overflow the stack in many languages. Use BFS with a queue and a distance array, or a DFS with an explicit stack. Same O(n) time either way.
What edge cases break solutions here?+
n = 1 with an empty edge list must return 0. Also watch off-by-one errors: the diameter counts edges, not vertices. Build the adjacency list in both directions since edges are undirected. A two-node tree should return 1.
How do I prepare for this in 48 hours?+
Write the two-BFS solution from scratch twice, with an adjacency list built from the edge array. Then write the single-DFS height version and compare. Test on a chain, a star, and n = 1. That covers the shapes this problem tends to throw at you.