Data Structures & AlgorithmsModerateLeetCode-style · TCS NQT

Children need greed factors [1, 2, 3, 5, 7]. Each child can receive at most one cookie, and a child is satisfied if the cookie size is at least their greed factor. Cookie sizes are [1, 3, 4, 6, 6] (each cookie used at most once). What is the maximum number of satisfied children?

  1. A. 5
  2. B. 3
  3. C. 6
  4. D. 4

Correct answer: D. 4

Sort both lists and match greedily: give the smallest cookie that satisfies the least greedy child still unmatched. Matching the largest cookies to the largest children (in arbitrary pairs) can waste a cookie on a child who would have accepted a smaller one.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic