Reported September 2023
Amazongreedy

Count Max Num Teams

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

You're looking at Amazon's team formation problem from September 2023. Sort the skill array, then greedily partition consecutive developers into teams of size teamSize. The constraint is simple: within each team, max skill minus min skill can't exceed maxDiff. After sorting, any valid team will be a contiguous slice, so you scan left to right and count how many non-overlapping windows of teamSize satisfy the constraint. This is a greedy pattern with a sorting setup. StealthCoder will handle the implementation if you blank on the exact loop structure during the live OA.

The problem

\ Amazon is hosting a team hackathon.\ \
\ 1. Each team will have exactly teamSize developers.
\ 2. A developer's skill level is denoted by skill[i].
\ 3. The difference between the maximum and minimum skill levels within a team cannot exceed a threshold, maxDiff.
\
\ \
Determine the maximum number of teams that can be formed from the contestants.\ \
Complete the function countMaxNumTeams which has the following parameters\ \
\ int skill[n]: the developers' skill levels
\ int teamSize: the number of developers to make up a team
\ int maxDiff: the threshold value.
\
\ \
int: the maximum number of teams that can be formed at one time\

Reported by candidates. Source: FastPrep

Pattern and pitfall

The core trick is realizing that sorting unlocks a greedy solution. Once sorted, a team is valid if and only if the last developer's skill minus the first developer's skill in that window is within maxDiff. You iterate through the array, and whenever you can form a valid team of teamSize consecutive developers, you take it and move your pointer forward by teamSize. Common pitfall: trying a sliding window or hash-based approach without sorting first. That wastes time. Another trap: forgetting to check the constraint before committing a team. The greedy choice is safe because sorting means any gap you skip will only get worse as you move forward. StealthCoder serves as your safety net if the greedy loop logic slips your mind under pressure.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Count Max Num Teams 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 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. 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.

Count Max Num Teams FAQ

Do I have to form teams of consecutive sorted developers?+

Yes. Once you sort by skill, any valid team must be a contiguous slice. Mixing non-consecutive developers would only widen the max-min gap, violating the constraint. Sorting collapses the problem into a straightforward iteration.

What if I can't form any teams?+

Return 0. It's possible that even the smallest consecutive window of teamSize violates maxDiff. Check your first window before entering the loop. No team formed means the answer is zero.

Is this a sliding window problem?+

No. Sliding window maintains overlapping windows and recomputes state. Here you greedily partition into non-overlapping teams. Once a team is formed, you jump forward by teamSize and start fresh. Very different rhythm.

How do I verify a team is valid in one check?+

After sorting, just compare skill[i + teamSize - 1] - skill[i] <= maxDiff for each candidate team starting at index i. One subtraction, one comparison. That's it.

Can I form overlapping teams or use a developer twice?+

No. Each developer belongs to at most one team. Once you assign teamSize developers to a team, they're out of the pool. That's why the greedy forward-scan approach works.

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