PPractick

DSA interview question

Assign Cookies

Easy · Algorithms · Topics: greedy, sorting

The problem

You're volunteering to organize a small classroom treat day. Each child has a "greed factor" — the minimum cookie size that would satisfy them — and you have a limited stash of cookies of various sizes. Each child can receive at most one cookie, and a cookie only satisfies a child if its size is greater than or equal to that child's greed factor. Given an array g (greed factors of children) and an array s (sizes of available cookies), find the maximum number of content children you can satisfy.

Function signature

int findContentChildren(vector<int>& g, vector<int>& s);

Test cases

g (greed)s (cookie sizes)OutputExplanation
[1,2,3][1,1]1only one cookie (size 1) can satisfy the child with greed 1
[1,2][1,2,3]2both children satisfied, e.g. greed 1 → cookie 1, greed 2 → cookie 2
[10,9,8,7][5,6,7,8]2greed 7 → cookie 7 or 8, greed 8 → the other; greed 9,10 unsatisfiable
[][1,2,3]0no children to satisfy
[1,2,3][]0no cookies available
[7,8,9,10][5,6,7,8]2same idea, ordering shouldn't matter since we'll sort anyway
[1,1,1][1,1,1]3every child gets exactly matched

(Note: this is a classic "greedy + sort" warm-up problem — deceptively simple, but interviewers use it to check if you can quickly justify WHY greedy works, not just implement it.)

How interviewers phrase it

Clarifying questions to ask

  1. Can a child receive more than one cookie? (No — confirm explicitly: at most one cookie per child.)
  2. Does "satisfies" mean cookie size must be >= greed factor, or strictly >? (Standard problem: >=.)
  3. Are g and s guaranteed non-negative? Could they contain duplicates? (Yes, duplicates are fine and common.)
  4. Is there any constraint on reusing cookies across children? (No — each cookie used at most once, matching at most one child.)
  5. What should be returned if either array is empty? (0, trivially.)

Follow-ups to expect

  1. "Why does sorting both arrays and using a greedy two-pointer approach work? Can you prove it?"
  2. "Can you solve it without sorting, in better than O(n log n)?"
  3. "What if you wanted to satisfy the MOST 'greedy' children preferentially instead of maximizing total count?"
  4. "What if each cookie could be split into smaller pieces to satisfy multiple children?"
  5. "What if cookies had different 'costs' and you had a budget constraint on top of sizes?"
  6. "Can you do this with a two-pointer approach instead of nested loops, and what's the complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in