DSA interview question
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.
int findContentChildren(vector<int>& g, vector<int>& s);
g (greed) | s (cookie sizes) | Output | Explanation |
|---|---|---|---|
[1,2,3] | [1,1] | 1 | only one cookie (size 1) can satisfy the child with greed 1 |
[1,2] | [1,2,3] | 2 | both children satisfied, e.g. greed 1 → cookie 1, greed 2 → cookie 2 |
[10,9,8,7] | [5,6,7,8] | 2 | greed 7 → cookie 7 or 8, greed 8 → the other; greed 9,10 unsatisfiable |
[] | [1,2,3] | 0 | no children to satisfy |
[1,2,3] | [] | 0 | no cookies available |
[7,8,9,10] | [5,6,7,8] | 2 | same idea, ordering shouldn't matter since we'll sort anyway |
[1,1,1] | [1,1,1] | 3 | every 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.)
>= greed factor, or strictly >? (Standard problem: >=.)g and s guaranteed non-negative? Could they contain duplicates? (Yes, duplicates are fine and common.)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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in