Assign Cookies With Matching Parity
Reported by candidates from Infosys's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Infosys reportedly asked this one in September 2026, and the input size is what gives it away. Both arrays can hit 30,000 elements, so trying every child against every cookie is a dead end. It's Assign Cookies with a parity twist, and the twist is the whole point. Split the problem by parity and the greedy you already know still works. If you've got an OA invite and you're short on time, this is a pattern you can lock in tonight. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment.
The problem
You are given two integer arrays g and s. g[i] is the greed factor of child i. s[j] is the size of cookie j. Assign at most one cookie to each child. Cookie j may be assigned to child i only when s[j] >= g[i] and s[j] and g[i] have the same parity: both even or both odd. Return the maximum number of children who can be assigned a cookie. Function findContentChildren(g: int[], s: int[]) → int Examples Example 1 g = [1,2,3] s = [1,2,3] return = 3 Each child can take the cookie of equal size. The pairs 1, 2, and 3 all have matching parity. Example 2 g = [1,3] s = [2,4] return = 0 Both children have odd greed, and both cookies are even, so no assignment is legal. Constraints 1 <= g.length <= 3 * 10^4. 0 <= s.length <= 3 * 10^4. 1 <= g[i], s[j] <= 2^31 - 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: a cookie can only go to a child with the same parity, so the two groups never interact. Split g and s into odd and even lists. Sort each list. For each group, run the classic two-pointer greedy: walk through cookies in ascending order, and if the current cookie is at least the current child's greed, assign it and advance the child pointer. Add the two counts. That's O(n log n) from sorting, which fits the 3 * 10^4 limit easily. The common pitfall is sorting everything together and checking parity on the fly, which wastes cookies on children they can't feed. Another miss is forgetting s can be empty. Values go up to 2^31 - 1, so watch overflow if you add anything in a typed language. If you freeze on the split, StealthCoder is the hedge during the live OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Assign Cookies With Matching Parity 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 Infosys's OA.
Infosys 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.
Assign Cookies With Matching Parity FAQ
What's the trick in Assign Cookies With Matching Parity?+
Partition both arrays by parity first. Odd children can only take odd cookies and even children only even ones. After that, each group is the standard sorted two-pointer greedy. Sum the two results. The parity rule just turns one problem into two independent ones.
How hard is this one really?+
Easy to medium. If you've seen the original Assign Cookies, the only new step is splitting by parity. The code is short. Most mistakes come from mixing parities in one pass or from skipping the sort before the greedy.
Why does brute force fail here?+
With up to 30,000 children and 30,000 cookies, checking every pair is about 900 million comparisons, and matching them optimally by trying combinations is worse. Sorting plus two pointers brings it to O(n log n), which runs comfortably at this size.
Can I do it without splitting into separate lists?+
Yes, but it's messier. You can sort each array and keep separate pointers per parity, advancing the right one as you scan. Splitting into two lists is cleaner and less error-prone, so use that unless you have a reason not to.
How do I prepare for this in 48 hours?+
Write the original Assign Cookies greedy from memory twice. Then add the parity split and test on the two examples, including the case that returns 0. Also test an empty cookie array. That covers the edge cases this Infosys question reportedly leans on.