The Fair Distribution of Cookies interview question asks you to distribute bags of cookies among children. Each bag contains a certain number of cookies, and you must give each bag to exactly one child. You want to distribute the bags such that the "unfairness" is minimized. Unfairness is defined as the maximum number of cookies received by any single child.
Companies like Microsoft and Amazon ask this Backtracking coding problem to test your ability to explore all possible distributions. It's a variation of the "Partition to K Equal Sum Subsets" or "Bin Packing" problem. It evaluates your skills in recursion, state management, and pruning. Since the number of bags is usually small (e.g., up to 8), an exponential backtracking solution is often the intended approach.
This problem is solved using Backtracking.
children[i] stores the total cookies child has received so far.Bags: [8, 15, 10, 20, 8], Children: .
For distribution problems with small , backtracking is the first thing to try. If the interviewer asks for an optimization, mention that this could be solved with Dynamic Programming using Bitmasking to avoid redundant calculations of the same "set of bags."
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Number of Work Sessions to Finish the Tasks | Medium | Solve | |
| Matchsticks to Square | Medium | Solve | |
| Beautiful Arrangement | Medium | Solve | |
| Campus Bikes II | Medium | Solve | |
| Maximum Compatibility Score Sum | Medium | Solve |