FastPrepKey Teams in Tree
Problems/Key Teams in Tree

Key Teams in Tree

Salesforce logoSalesforceFULLTIMEOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

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 = 4edges = [[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 = 4edges = [[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.

More Salesforce Online Assessment questions

  • Count Subarrays With Product Equal to KOA · Seen Nov 2026
  • Minimum No-Repeat Segments After One Character RemovalOA · Seen Nov 2026
  • Get Message StatusOA · Seen Nov 2026
  • Minimum Unique-Character Segments After DeletionOA · Seen Sep 2026
  • Run-Length String CompressionOA · Seen Sep 2026
  • Maximum Requests in WindowOA · Seen Sep 2026
  • Final Pod Counts After LogsOA · Seen Aug 2026
  • Minimum Removals to Balance ArrayOA · Seen Aug 2026
  • Is This Date a Weekend?OA · Seen Aug 2026
  • Maximum Barbell WeightOA · Seen Jul 2026

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
All Salesforce interview questionsSee Salesforce hiring insights
Saved
class Solution {
    public int[] keyTeamsInTree(int n, int[][] edges) {
      // write your code here
    }
}
Checking account…