Key Teams in Tree
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Salesforce reported this one in March 2026, and it's a tree diameter problem wearing an org-chart costume. "Key Teams in Tree" asks which nodes sit at the end of at least one longest path. If your OA is in a day or two, the good news is you only need BFS or DFS and one extra pass. Don't hunt for a fancy structure. StealthCoder is there as a quiet safety net during the live OA if your mind goes blank, but the pattern below is short enough to carry in your head.
The problem
You are given a tree with n nodes labeled from 0 to n - 1. The tree represents an organizational hierarchy. A node is a key team if it is an endpoint of at least one longest path in the tree. A longest path in a tree is also called a tree diameter. Return a binary array answer of length n, where answer[i] = 1 if node i is a key team, and 0 otherwise. Function keyTeamsInTree(n: int, edges: int[][]) → int[] Examples Example 1 n = 4 edges = [[0, 1], [1, 2], [2, 3]] return = [1, 0, 0, 1] The only diameter is the path from node 0 to node 3, so those two nodes are marked. Example 2 n = 4 edges = [[0, 1], [0, 2], [0, 3]] return = [0, 1, 1, 1] Every longest path goes between two leaves, so nodes 1, 2, and 3 are all endpoints of some diameter. Constraints 1 <= n edges.length == n - 1 The edges form a tree.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: find the diameter length D, then mark every node that is at distance D from some other node. Do BFS from node 0 to find the farthest node A. BFS from A to get distances distA and find the farthest node B. BFS from B to get distB. A node is an endpoint of some diameter if max(distA[i], distB[i]) == D. That works because the farthest node from any vertex is always a diameter endpoint. The common pitfalls are marking only A and B, which fails on the star example where all leaves count, and forgetting n = 1, where the single node has diameter 0 and probably counts as an endpoint, so check how your code handles it. Use iterative BFS to avoid recursion depth trouble on a path-shaped tree. Total work is three traversals, O(n). If you blank on the live OA, StealthCoder can surface this three-BFS approach while you keep typing.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Key Teams in Tree 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 would have shipped this the night before his JPMorgan OA if he'd had it.
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 would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Key Teams in Tree FAQ
What's the trick in Key Teams in Tree?+
Reduce it to tree diameter. Run BFS from any node to find a farthest node A, BFS from A to find the diameter length D and farthest node B, then BFS from B. Mark node i if max(distA[i], distB[i]) equals D. Three passes, linear time.
Why isn't marking just the two diameter endpoints enough?+
A tree can have many diameters. In the star example with center 0, every pair of leaves forms a longest path, so nodes 1, 2, and 3 are all key teams. You need every node whose farthest distance equals the diameter, not just one pair.
How hard is this really for a Salesforce OA?+
Medium. If you've seen tree diameter, it's quick. The extra step of collecting all endpoints is what catches people. Once you know the farthest-node property, the code is three short BFS calls and a loop to build the answer array.
Should I use DFS or BFS?+
BFS is safer. It's iterative, so a chain of many nodes won't blow the recursion stack. You also get distances directly from each traversal. DFS works with an explicit stack or a tree DP, but BFS is less code and fewer bugs.
How do I prepare for this in 48 hours?+
Write tree diameter by two BFS passes from scratch, then add the third pass and the max-distance check. Test on a path, a star, and a single node. Build the adjacency list from edges first. That covers nearly every case this problem can throw at you.