Quick Overview

Maximize satisfied children by greedily assigning sorted cookie sizes, with an exchange proof, empty-input handling, and optional assignment reconstruction.

Assign Cookies to Maximize Satisfied Children

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Each child has a minimum acceptable cookie size, and each cookie has a size. A child is satisfied by one cookie at least as large as that child's requirement. Each cookie can be assigned to at most one child. Return the maximum number of satisfied children. ### Clarifying Questions to Ask - Can each child receive at most one cookie, and are cookie sizes indivisible? - May the input arrays be sorted in place? ```hint Save large cookies when possible Consider satisfying the least-demanding remaining child with the smallest cookie that can work. ``` ### What a Strong Answer Covers - Sorting both requirements and cookie sizes. - Advancing past too-small cookies without discarding an unsatisfied child. - A greedy exchange argument and empty-input handling. ### Follow-up Questions - How would you return one actual assignment instead of only the count? - Why does assigning the largest cookie to the easiest child risk wasting capacity?

Overview: Maximize satisfied children by greedily assigning sorted cookie sizes, with an exchange proof, empty-input handling, and optional assignment reconstruction.

Read the full Amazon Software Engineer interview experience this question came from

Each child has a minimum acceptable cookie size in `requirements`, and each cookie has a size in `cookies`. A child is satisfied if assigned a cookie of at least its required size. A cookie is indivisible, and each child and cookie can participate in at most one assignment. Return the maximum number of children that can be satisfied.

Constraints

  • requirements and cookies may each contain 0 through 2^31 - 1 entries.
  • Every requirement and cookie size is an integer from 0 to 2^53 - 1, inclusive.

Examples

Input: ([4, 1, 7], [1, 7, 3])

Expected Output: 2

Explanation: Give sizes 1 and 7 to children who require 1 and 7. The size 3 cookie cannot satisfy the child who requires 4.

Input: ([2, 5], [5, 2])

Expected Output: 2

Explanation: Giving each child a cookie exactly at its requirement satisfies both.

Hints

  1. If a cookie is too small for one child, identify which other children it can still satisfy.

Assign Cookies to Maximize Satisfied Children

hardAmazonSoftware Engineer

Each child has a minimum acceptable cookie size in requirements, and each cookie has a size in cookies. A child is satisfied if assigned a cookie of at least its required size. A cookie is indivisible, and each child and cookie can participate in at most one assignment. Return the maximum number of children that can be satisfied.

Examples

Example 1
Input
([4, 1, 7], [1, 7, 3])
Output
2
Notes
Give sizes 1 and 7 to children who require 1 and 7. The size 3 cookie cannot satisfy the child who requires 4.
Example 2
Input
([2, 5], [5, 2])
Output
2
Notes
Giving each child a cookie exactly at its requirement satisfies both.

Constraints

  • •requirements and cookies may each contain 0 through 2^31 - 1 entries.
  • •Every requirement and cookie size is an integer from 0 to 2^53 - 1, inclusive.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def max_satisfied_children(requirements: list[int], cookies: list[int]) -> int:
Sample call
max_satisfied_children([4, 1, 7], [1, 7, 3])
Language
Case 1
Input
([4, 1, 7], [1, 7, 3])
Expected
2
Case 2
Input
([2, 5], [5, 2])
Expected
2

Browse More Questions

Show the approach

Approach

Sort the children by requirement and the cookies by size. Scan cookies from smallest to largest while tracking the least demanding child still waiting. Skip a cookie if it is too small for that child, because it is too small for all other waiting children. Otherwise assign it to that child and advance to the next child.

A cookie that can satisfy the least demanding child is safe to assign there. In an optimal matching of the remaining children and cookies, if both are matched differently, exchange the assignments: the least demanding child takes this cookie, and the other child takes the at-least-as-large cookie that previously served the least demanding child. If one is unmatched, redirect the existing assignment, or add the match when both are unmatched. Thus some optimum uses the greedy match. Repeating this choice gives a maximum count.

Time complexity:
O(n log n + m log m) for n children and m cookies.
Space complexity:
O(n + m) auxiliary space for sorted copies.