Magicsheet logo

Assign Cookies

Easy
55.3%
Updated 6/1/2025

Assign Cookies

What is this problem about?

The Assign Cookies interview question is a greedy allocation problem. You have a list of children with specific greed factors and a list of cookies with specific sizes. A child will be content if they receive a cookie with a size greater than or equal to their greed factor. Your goal is to maximize the number of content children. This Assign Cookies coding problem is a perfect introduction to greedy optimization.

Why is this asked in interviews?

Companies like Apple and Amazon use this as a "warm-up" question. It tests if you can identify that a local optimal choice (giving the smallest possible sufficient cookie to the least greedy child) leads to a global optimal solution. It also checks your ability to use two-pointer logic on sorted data.

Algorithmic pattern used

This follows the Array, Sorting, Two Pointers, Greedy interview pattern. By sorting both the children's greed factors and the cookie sizes, you can iterate through both lists once to make the best possible assignments.

Example explanation

Children's greed: [1, 2, 3], Cookies: [1, 1].

  1. Sort both: They are already sorted.
  2. Child 1 (Greed 1): Cookie 1 (Size 1) is enough. Assign it. (1 content child).
  3. Child 2 (Greed 2): Cookie 2 (Size 1) is NOT enough. This cookie cannot satisfy any remaining child either.
  4. Result: 1 content child.

Common mistakes candidates make

  • Not Sorting: Trying to assign cookies without sorting, which makes it impossible to guarantee that you aren't "wasting" a large cookie on a child who could have been satisfied by a smaller one.
  • Inefficient Search: Using nested loops (O(N * M)) instead of two pointers (O(N log N + M log M)).
  • Wasteful Allocation: Giving a huge cookie to a child with low greed when a smaller cookie was available.

Interview preparation tip

Greedy problems almost always require sorting the input. If you're trying to match two sets of values to satisfy a condition, sort them both and use the two-pointer approach to find the optimal pairings.

Similar Questions