FastPrepCollect Opportunity Data in a Tree
Problems/Collect Opportunity Data in a Tree

Collect Opportunity Data in a Tree

Salesforce logoSalesforce● HardOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given an undirected tree with n nodes labeled from 0 to n - 1. The tree is defined by n - 1 edges.

Each node represents a branch office. You are also given a binary array opportunityData of size n, where:

  • opportunityData[i] = 1 means branch i contains important data.
  • opportunityData[i] = 0 means it does not.

You may start at any node. You can traverse edges in both directions. You must start and end at the same node.

Special Rule: When you are at a node, you can collect data from all nodes within distance <= 2 edges from your current location.

Return the minimum number of edges you must traverse to collect all important data.

Function

collectOpportunityDataInTree(n: int, edges: int[][], opportunityData: int[]) → int

Examples

Example 1

n = 10edges = [[0,1],[0,2],[0,5],[1,3],[1,4],[3,9],[5,6],[6,7],[7,8]]opportunityData = [0,0,1,1,0,0,0,0,1,1]return = 6

Nodes 2, 3, 8, and 9 contain important data.

One optimal strategy:

  • Start at node 1.
  • From node 1, collect data at 3 and 9 (within distance 2).
  • Travel to node 0 and collect 2.
  • Travel toward node 6 to collect 8.
  • Return to the starting node.

Total edges traversed = 6.

Constraints

  • 2 <= n <= 10^5
  • edges.length == n - 1
  • The input graph is a valid tree
  • opportunityData[i] is either 0 or 1

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

Similar questions at other companies

  • Binary Tree CamerasAmazon · OA · PHONE SCREEN
  • Collect CoinsCisco · OACisco interview questions
  • Uber Circular Shuttle OptimizationUber · OAUber interview questions
  • Minimum Flips to Match Expected Binary Values in a TreeUKG · OAUKG interview questions
  • Save the UniverseRubrik · OARubrik interview questions
  • Equalize Server LatencyMicrosoft · OAMicrosoft interview questions
  • Equalize Server LatencyTikTok · OATikTok interview questions
  • Limit an Organization Tree's HeightRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
  • Minimum Leaf Path Sum over All RootsInfosys · OAInfosys interview questions
  • Part 2: Batch Access ReviewScale AI · OAScale AI interview questions

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 collectOpportunityDataInTree(int n, int[][] edges, int[] opportunityData) {
      // write your code here
    }

}
Checking account…