Assign Cookies
An easy Greedy interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Greedy
- Sheets
- 1
- Core for
- 3 roles
The problem
You are given an array of cookies where each element represents the size of a cookie, and a greed factor array where each element represents the minimum cookie size a child is content with. Assign one cookie to each child to satisfy as many children as possible. Return the maximum number of content children.
Example 1
- Input
- g = [2,3,4], s = [2,2]
- Output
- 1
- Why
- A size-2 cookie satisfies only the child with greed 2.
Example 2
- Input
- g = [2,3], s = [2,3,4]
- Output
- 2
- Why
- Cookie 2 goes to greed 2 and cookie 3 to greed 3.
Constraints
- 1 <= g.length, s.length <= 3 * 10^4
- 1 <= g[i], s[j] <= 2^31 - 1
How to think about it
Updated 2026-09-29Giving a huge cookie to an readily satisfied child wastes potential capacity that could have satisfied a greedier one. Pair the smallest adequate cookie with the least greedy child to conserve larger cookies for higher greed demands.
Approaches, worst first
Brute force search
time O(n * m) · space O(m)
For each child, scan the cookie array to find the smallest unused cookie that satisfies them, marking cookies as used. Repeated linear scans over unused items degrade performance.
Sort both arrays and two pointersWrite this one
time O(n log n + m log m) · space O(1)
Sort greed factors g and cookie sizes s ascending. Advance a cookie pointer on every step. Whenever the current cookie satisfies the current child, advance the child pointer. The final child pointer position represents the maximum satisfied children.
Where people lose marks · 2
- Advancing the child pointer when the cookie is too small; the child must remain waiting for a larger cookie while the cookie pointer advances.
- Assuming the number of cookies equals the number of children; either array can exhaust first, so the loop condition must check both bounds.
The theory behind it
Greedy — the ground this problem stands on. All Greedy problems
What Greedy is
A greedy algorithm makes the best-looking choice available right now, at every step, without ever looking back or second-guessing its decision. Think of a cashier making change by handing over the largest possible coin first, repeatedly, until the total is reached. Unlike dynamic programming, which saves and compares answers to multiple overlapping paths, a greedy strategy commits to one immediate option and keeps moving forward.
When to reach for it
Reach for greedy when problems ask for minimum jumps, interval scheduling, assigning resources to maximize satisfaction, or finding fractional values. Key signals include sorted orders where greedily taking the next item never hurts future options, or gas station round trips where running balances prove reachability. If you can prove that taking the immediate best choice never leaves you worse off than any alternative, greedy gives the fastest answer.
How the pattern works
Start by sorting the input to bring the most promising candidates to the front. At each position, evaluate your local rule, take the best available piece, and update your running state. The crucial mental step is proving the greedy choice property: demonstrate that picking this immediate winner cannot block a better global solution down the road. If choosing an item now forces you to reconsider past decisions when conditions change later, greedy fails and you must switch to dynamic programming instead.
More Greedy problems
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .