Maximum Equal Parts for Prefixes
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in June 2026, and the setup is plain: a string of package labels, A to Z, and for every prefix you return the max number of equal parts. Each part needs the same count of every letter. If "ABAB" giving [1, 1, 1, 2] looks easy, the catch is doing it for all n prefixes without blowing up. It's a counting and divisibility problem wearing a string costume. If you blank during the live OA, StealthCoder sits invisibly on your screen as a safety net. But the idea below is short enough to own.
The problem
A team at Amazon is working to ensure all packages are correctly sorted for delivery. Each package has a label represented by an uppercase English letter. The full list of labels is given as the string packages, where the ith character is the label of the ith package.
To optimize the sorting process, the team wants to analyze each prefix of the string packages (from length 1 to n) and determine the maximum number of equal parts it can be divided into. Each part must satisfy the following conditions:
Each part must have the same frequency of every character as every other part in that division
Given a string packages, calculate for each prefix t (from length 1 to n), the maximum number of equal parts into which the prefix can be divided, such that each part has the same number of occurrences of each character.
Function
maximumEqualParts(packages: String) → int[]
Examples
Example 1
packages = "ABAB"
return = [1, 1, 1, 2]
Given, packages = "ABAB".
In the given example t represents prefix string and length represents the length of the prefix string.
Return [1, 1, 1, 2] as the answer.
Constraints
packages consists only of uppercase English letters ('A' to 'Z').
The answer is computed for every prefix of packages of length 1 to n, where n is the length of packages.
For each prefix, the maximum number of equal parts is at least 1, since a prefix can always be treated as a single undivided part.Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: a prefix of length L can split into k equal parts only if k divides every letter's count in that prefix. So the answer for a prefix is the gcd of all 26 letter counts, ignoring zeros. Keep a running count array, update one letter per step, then take the gcd of the nonzero counts. That's O(26 * n) with gcd cost, which is fine. Check it on "ABAB": at length 4 counts are A=2, B=2, gcd 2. At length 3 counts are 2 and 1, gcd 1. The pitfall is brute-forcing every divisor of L and re-checking the prefix, which goes quadratic. Another miss is letting a zero count drag the gcd wrong. Skip zeros, or note gcd(0, x) = x. Also remember the order of characters doesn't matter, only frequencies. StealthCoder is your hedge if the gcd idea won't come when the clock is running.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Maximum Equal Parts for Prefixes 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Equal Parts for Prefixes FAQ
What's the trick for Maximum Equal Parts for Prefixes?+
Count each letter as you scan. For each prefix, the answer is the gcd of all nonzero letter counts. A split into k equal parts needs k to divide every count, and the largest such k is the gcd. One pass, no divisor enumeration.
How hard is this problem really?+
Easy to medium. Once you see the gcd idea, the code is about ten lines. The difficulty is spotting it. If you try to actually build the parts or check substrings, you'll overcomplicate it and likely time out.
What's the time complexity I should aim for?+
O(26 * n) with gcd overhead, which is effectively linear in the string length. Update one counter per character, then fold gcd across the 26 counts. Anything that rescans each prefix per divisor is too slow for large inputs.
How do I handle letters that haven't appeared yet?+
Skip zero counts when computing the gcd, or start the gcd at 0 since gcd(0, x) equals x. If you include zero as a real divisor target, you'll get wrong answers on prefixes with only a few distinct letters.
How do I prepare for this in 48 hours?+
Write the running-count plus gcd solution once from scratch and test it on "ABAB" and an all-same-letter string like "AAAA", which should give [1, 2, 3, 4]. Also review similar prefix-frequency problems. That covers the core pattern Amazon is testing here.