Collect Opportunity Data in a Tree
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
With n up to 10^5, the Salesforce OA question "Collect Opportunity Data in a Tree", reported in February 2026, kills any brute force that tries every start node and every route. You're on a tree, you need a closed walk, and each stop collects data within 2 edges. That's the twist. It looks like a graph walk but it's really a tree pruning problem. If you have this OA in the next few days, learn the shape of it now. StealthCoder sits invisibly as a safety net on the live OA if your mind goes blank halfway through.
The problem
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 = 10 edges = [[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
Reported by candidates. Source: FastPrep
Pattern and pitfall
This is the known LeetCode problem "Collect Coins in a Tree" in new clothes. The trick: a closed walk on a tree costs 2 times the number of edges you cover, because you cross each one twice. So you just need to find which edges you must cover. Step one: repeatedly trim leaves that have no data, using a queue and degree counts. Step two: trim two more layers of leaves, since standing 2 edges away collects them. Whatever nodes remain form the subtree you have to walk. If k nodes remain, the answer is 2 * (k - 1), or 0 if k is 0 or 1. The common pitfall is simulating the walk or picking a start node, which is slow and unnecessary. Another is trimming the two extra layers before removing the empty leaves. Do it in that order. Total work is O(n) with a BFS-style leaf peel. If you blank on the layer trimming during the live OA, StealthCoder is your hedge.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Collect Opportunity Data in a 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as collect coins in a tree. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Salesforce's OA.
Salesforce reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Collect Opportunity Data in a Tree FAQ
What's the trick in the Salesforce Collect Opportunity Data in a Tree question?+
Don't simulate the walk. Peel off leaves with no data first, then peel two more leaf layers because radius 2 covers them. The remaining nodes form the required subtree. The answer is 2 times its edge count, since a closed walk crosses each edge twice.
How hard is this problem really?+
It's a hard-tagged idea but short to code once you see it. The code is a degree array, a queue, and a few loops. The difficulty is the insight that the start node doesn't matter and that you can prune instead of search.
What happens if no node has data, or only one remains?+
Return 0. If every node is trimmed or only one node is left after the pruning, you don't need to move at all. Handle this explicitly with max(0, 2 * (remaining - 1)) so you don't return a negative number on small inputs.
Why does the order of trimming matter?+
First remove leaves with value 0 repeatedly, until every leaf has data. Then do exactly two rounds of removing all current leaves. If you do the radius trimming first, you can delete a leaf that still had data to be collected from a deeper position, giving a wrong answer.
How do I prepare for this in 48 hours?+
Write the leaf-peeling BFS from scratch twice with degree counts. Test it on the sample, which should return 6, plus a single-node-data case and an all-zero case. Then review similar tree pruning problems. Aim for O(n) time so n = 10^5 passes.