Bird
Raised Fist0

Suppose now each cookie can be assigned to multiple children (reusable cookies). Which modification to the original greedy algorithm correctly computes the maximum number of content children?

hard🎤 Interviewer Follow-up Q15 of Q15
Greedy Algorithms - Assign Cookies
Suppose now each cookie can be assigned to multiple children (reusable cookies). Which modification to the original greedy algorithm correctly computes the maximum number of content children?
ASort both arrays and increment both pointers i and j on assignment as before
BUse the brute force nested loops approach to try all assignments since greedy fails with reusable cookies
CSort greed array only and assign the largest cookie to each child without sorting cookies
DKeep sorting arrays, but do not increment cookie pointer j when a cookie is assigned; only increment child pointer i
Step-by-Step Solution
  1. Step 1: Understand reuse effect

    Cookies can be assigned multiple times, so cookie pointer j should not advance on assignment.
  2. Step 2: Modify greedy accordingly

    Keep sorting both arrays, but only increment child pointer i when a cookie satisfies a child; j stays to reuse the same cookie.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Not incrementing j allows cookie reuse [OK]
Quick Trick: Do not advance cookie pointer on assignment for reuse [OK]
Common Mistakes:
MISTAKES
  • Incrementing both pointers breaks reuse
  • Using brute force unnecessarily
  • Ignoring sorting leads to suboptimal matches
Trap Explanation:
PITFALL
  • Naively incrementing cookie pointer breaks reuse assumption, causing undercount.
Interviewer Note:
CONTEXT
  • Tests ability to adapt greedy logic to problem variants with relaxed constraints.
Master "Assign Cookies" in Greedy Algorithms

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Greedy Algorithms Quizzes