Reported September 2026
Amazonbinary search

Aggressive Cows

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon reported this one in September 2026, and the detail that matters is in the examples: stalls [10,1,2,7,5] come unsorted, and the answer 4 only shows up after you sort to 1, 5, 10. Aggressive Cows asks you to place exactly the given number of cows so the smallest gap between any two is as large as possible. It's a binary search on the answer, not a search over placements. If the OA is in a day or two, learn that shape cold. And if you blank mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and reads the problem for you.

The problem

Given distinct integer stall positions stalls and an integer cows, place exactly cows cows in different stalls.
Return the largest possible value of the minimum distance between every pair of placed cows.

Function
aggressiveCows(stalls: int[], cows: int) → int

Examples
Example 1
stalls = [1,2,4,8,9]
cows = 3
return = 3
Placing cows at 1, 4, and 8 gives a minimum distance of 3, which cannot be improved.
Example 2
stalls = [10,1,2,7,5]
cows = 3
return = 4
After sorting, positions 1, 5, and 10 achieve minimum distance 4.
Example 3
stalls = [0,5]
cows = 2
return = 5
Both stalls must be used, so their distance is the answer.

Constraints
2 <= stalls.length <= 10^5.
2 <= cows <= stalls.length.
0 <= stalls[i] <= 10^9.
All stall positions are distinct.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: don't search placements, search the distance. Sort the stalls. Binary search d between 1 and max minus min. For each d, run a greedy check: put the first cow in the first stall, then drop the next cow at the first stall at least d away from the last one. If you place at least cows cows, d is feasible, so try bigger. Otherwise go smaller. Feasibility is monotonic, which is why binary search works. Cost is O(n log n) for the sort plus O(n log 10^9) for the search, fine for n up to 10^5. Common pitfalls: forgetting to sort, using a check that counts cows wrong, off-by-one in the loop bounds, and returning the wrong boundary. Track the last feasible d. If your head goes blank on the live Amazon OA, StealthCoder is the hedge that hands you the structure fast.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Aggressive Cows 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 passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Aggressive Cows FAQ

What's the trick in Aggressive Cows?+

Binary search on the answer. Sort stalls, then guess a minimum distance d and greedily check if you can place all cows with gaps of at least d. Feasible means try larger, infeasible means try smaller. The greedy check is a single pass over the sorted array.

How hard is this really?+

Medium to hard, mostly because the pattern isn't obvious. The code is short once you see it: a sort, a greedy check function, and a binary search loop. If you've done binary search on answer problems before, it takes about 15 minutes.

Why does greedy placement work in the check?+

Placing each cow at the earliest stall that's at least d from the previous one leaves the most room for the remaining cows. Any valid placement can be shifted left to match the greedy one without breaking the gap. So if greedy fails, nothing works.

What are the edge cases to test?+

Test cows equal to stalls.length, where the answer is the smallest adjacent gap. Test two stalls like [0,5], where the answer is their difference. Test unsorted input like Example 2. Also check large positions up to 10^9, where the search bounds still fit in a normal int.

How do I prepare in 48 hours?+

Write this from scratch twice. Then do two related problems that use the same binary search on answer shape, such as capacity or split-array style problems. Focus on the feasibility function and the boundary update. Don't memorize code, memorize the monotonic check idea.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it