Collect Branches for a Bird Nest
Reported by candidates from Visa's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Visa OA reported in August 2026 dresses up as a bird story, but it's a simulation with two pointers. You walk outward from index n, grab the nearest branch on one side, flip sides, and stop once the total hits 100. If you've got an assessment coming in the next few days, the whole question is getting the direction-switching rules exactly right. The tricky part isn't the algorithm, it's the edge cases around skipped searches and the starting cell. StealthCoder sits invisible on your screen as a safety net if you blank mid-assessment, but the logic below is simple enough to hold in your head.
The problem
You are given an integer array forest and a positive integer n, the bird's zero-based starting index. A positive value forest[i] is the length of the branch at index i; 0 means that no branch is there. The bird builds a nest at its starting position. The nest begins with total branch length 0. The starting cell is never searched or collected, even if forest[n] is positive. Follow these rules until the collected length is at least 100: Search to the right first, then alternate left and right searches. Every search begins from the same original index n. In the chosen direction, collect the nearest remaining positive-length branch. Remove that entire branch, add its length to the nest, and return to index n. A branch can be collected only once. If no branch remains in the chosen direction, skip that search and switch to the other direction. Stop immediately after a pickup makes the total length at least 100; do not make another search. The sum of all branch lengths outside index n is at least 100, so the nest can always be completed. Return the zero-based indices of the collected branches, in pickup order. Function collectBranches(forest: int[], n: int) → int[] Examples Example 1 forest = [0,45,0,0,30,0,40] n = 3 return = [4,1,6] From index 3, the right search collects index 4 for a total of 30. The left search collects index 1, raising the total to 75. The next right search collects index 6, bringing the total to 115, so the bird stops. Example 2 forest = [20,30,50,80] n = 3 return = [2,1,0] Index 3 is the starting cell, so its branch is excluded. Every right search is skipped because there are no cells to the right. The successive left pickups are indices 2, 1, and 0, with totals 50, 80, and 100. Constraints 2 <= forest.length <= 10^5. 0 <= forest[i] <= 100. 1 <= n < forest.length. The sum of forest[i] over all i != n is at least 100.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Don't rescan from n on every pickup. That's the trap. With up to 10^5 cells, rescanning gives O(n^2) in the worst case. Keep two pointers instead: r starts at n+1 and moves right, l starts at n-1 and moves left. Each pointer only moves forward, so the total work is O(n). Track a turn flag that starts on right. On a turn, advance that pointer past zeros. If it finds a positive branch, record the index, add its length, and stop if the total is at least 100. If it runs off the array, skip and use the other side. After a successful pickup, flip the turn. After a skip, the other side goes. The pitfalls are including index n itself, flipping the turn wrongly after a skip, and checking the total before recording the index. StealthCoder is your hedge if the switching logic tangles live, but a clean two-pointer loop handles both examples.
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 Branches for a Bird Nest 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
You've seen the question.
Make sure you actually pass Visa's OA.
Visa 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 Branches for a Bird Nest FAQ
What's the trick in the Visa branch collection problem?+
Use two pointers that move outward from n, one right and one left, and never reset them. Since a collected branch is removed and the nearest remaining one is always further out, each pointer only moves one way. That makes it linear instead of quadratic.
How hard is this OA really?+
Easy to medium. There's no fancy data structure. The difficulty is reading the rules carefully: right first, alternate, skip when a side is empty, ignore index n, and stop immediately at 100 or more. Most failures are off-by-one or turn-order bugs.
What happens when one side runs out of branches?+
You skip that search and the other direction goes. In Example 2 every right search is skipped because n is the last index, so all pickups come from the left. Just loop until total reaches 100, and the guarantee ensures it ends.
Should the starting cell ever be collected?+
No. Even if forest[n] is positive, it's never searched or collected. Start the right pointer at n+1 and the left at n-1. Example 2 shows this: index 3 holds 80 but isn't picked, and the answer is [2,1,0].
How do I prepare for this in 48 hours?+
Write the two-pointer loop from scratch and trace both examples by hand. Then test edge cases: n at index 1, all branches on one side, and a branch that lands exactly on 100. Practice stopping the loop right after the pickup that crosses 100.