TURO OA Interview Question: App to Help Grandparents Divide Their Giving Budget

17 Views
No Comments

Build an app to help grandparents divide their giving budget among their grandchildren. Write a program that calculates the number of lucky gifts (equal to 8) based on the budget and the number of grandchildren.

How it works

Many rules, mixing tradition and superstition, govern this gift.

  • Gifts must not contain the amount 4, as this sounds like “death”.
  • It is auspicious to give an amount of 8, as this sounds like “fortune”.
  • It would be wrong to give nothing to any of the grandchildren.

So your algorithm should return the number of gifts equal to following these rules:

  • Spend the entire budget, unless there is enough budget to give 8 to everyone.
  • Do not give any 4; by tradition, the budget will never be 4.
  • Do not give any 0, unless there is not enough money for all giftees.
  • Give a maximum amount of 8 once the above rules are met.

Examples

  • Classic budget: money = 11, giftees = 2. One lucky grandchild could get 8 and the other could get 3, so the function must return 1.
  • Perfect budget: money = 32, giftees = 4. It is possible to give 8 to all the grandchildren, so we must return 4.
  • Budget with a four: money = 12, giftees = 2. If the first grandchild gets 8 then the second would get 4, which is forbidden. Whatever the choice (7 and 5, or 6 and 6), nobody will get an amount of 8, so we must return 0.

This problem asks for a greedy allocation under special cultural rules: give a total budget to a number of grandchildren while maximizing how many receive exactly 8. If the budget can cover 8 for everyone, the answer is simply the number of grandchildren. Otherwise, the allocation must avoid any gift of 4 and, unless impossible, avoid zeros as well. The examples illustrate the logic: 11 split across 2 grandchildren can yield one gift of 8, 32 across 4 grandchildren can yield four gifts of 8, and 12 across 2 grandchildren cannot produce any 8 without violating the rules.

END
 0