Magicsheet logo

Distribute Money to Maximum Children

Easy
100%
Updated 6/1/2025

Asked by 4 Companies

Distribute Money to Maximum Children

What is this problem about?

The Distribute Money to Maximum Children coding problem asks you to distribute a total amount of money among children such that every child gets at least 1 dollar. You want to maximize the number of children who receive exactly 8 dollars. There are two additional constraints: no child can receive exactly 4 dollars, and if there is money left over, it must all be distributed.

Why is this asked in interviews?

Companies like Apple and Amazon use this "Easy" question to test basic Greedy interview patterns and edge-case handling. It evaluation whether you can prioritize a specific goal (giving out 8s) while respecting multiple constraints. It’s a "puzzle" problem that checks for logical thoroughness rather than complex data structures.

Algorithmic pattern used

This is a Greedy problem with Case Analysis.

  1. Initial check: If money < children, return -1 (can't give everyone $1).
  2. Subtract $1 from every child to satisfy the basic rule. remaining = money - children.
  3. Try to give as many children as possible an additional 7(tomaketheirtotal7 (to make their total 8). count = min(children, remaining / 7).
  4. Analyze the remainder:
    • If remaining % 7 == 3 and only one child is left (who would get 1+3=41 + 3 = 4), you must reduce the count of 8s by 1 to avoid the forbidden $4.
    • If all children have 8andthereisstillmoneyleftover,youmustgiveallthatmoneytothelastchild,meaningtheynolongerhaveexactly8 and there is still money left over, you must give all that money to the last child, meaning they no longer have exactly 8. Reduce count by 1.

Example explanation

Money = 20, Children = 3.

  1. Give $1 to each: Money left = 17.
  2. Give $7 to Child 1: Money left = 10. Total 8s = 1.
  3. Give $7 to Child 2: Money left = 3. Total 8s = 2.
  4. One child left. They have 1.Theywouldgettheremaining1. They would get the remaining 3, making their total $4. This is forbidden.
  5. Reduce count of 8s to 1. Result: 1.

Common mistakes candidates make

  • Ignoring the 4 dollar rule: Failing to handle the specific case where the remainder forces a child to have $4.
  • Negative Money: Not checking the baseline money >= children condition.
  • Total distribution: Forgetting that all money must be given away, even if it "ruins" an existing $8.

Interview preparation tip

For "Greedy" problems with several rules, always handle the most restrictive rules first. Start by giving everyone the minimum, then allocate the "premium" amounts, and finally adjust for edge cases.

Similar Questions