Distribute an integer budget evenly without exceeding amounts owed

Quick Overview

Distribute an integer budget as evenly as possible while respecting each recipient's debt cap. Handle uneven debts, leftover units, zero amounts, and excess funds without requiring one particular choice of remainder recipients.

Distribute an integer budget evenly without exceeding amounts owed

Company: Gusto

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Write a function that distributes an integer budget `total` among recipients. A map gives the nonnegative integer amount owed to each recipient. Every allocation must be an integer, must not exceed the recipient's amount owed, and must be as even as possible. For this exercise, interpret evenness as max-min fairness: if one recipient receives at least two more units than another, the lower-paid recipient must already have received their full amount owed. Use as much of the budget as the total debt allows. If several unsatisfied recipients can receive the final leftover units, any such assignment is acceptable. For example, with amounts owed `{a: 10, b: 10, c: 10, d: 10}`, a budget of `40` gives ten to each recipient, while `20` gives five to each. With amounts owed `{a: 2, b: 10, c: 10, d: 10}`, budget `21` can give `{a: 2, b: 6, c: 7, d: 6}`, and budget `22` can give `{a: 2, b: 7, c: 7, d: 6}`. Other choices of who receives the extra unit are equally valid. ```hint Separate debt limits from fairness A recipient whose debt is fully paid no longer participates in dividing the remaining budget. ``` ### Constraints and Clarifications Assume `total >= 0` and all amounts owed are nonnegative integers. The map has unique recipient identifiers. If total funds exceed all debt, leave the excess unallocated. These domain and surplus policies are exercise assumptions to confirm before coding. ### Clarifying Questions - May any eligible recipient receive a remainder unit, or is a business rule required for repeated allocations? - Should recipients owed zero remain in the returned map with zero allocated? - How should the caller receive any unused budget when total funds exceed the debt? ### What a Strong Answer Covers - Integer allocation, debt caps, budget conservation, and the stated fairness rule - A complete function and a trace covering recipients with unequal debt limits - Complexity and behavior for zero funds, zero debt, and excess budget - The distinction between one valid implementation choice and a required tie policy ### Follow-up Questions - How would you make remainder assignments fair over many repeated distributions? - How would the algorithm change if recipients had different priority weights?

Overview: Distribute an integer budget as evenly as possible while respecting each recipient's debt cap. Handle uneven debts, leftover units, zero amounts, and excess funds without requiring one particular choice of remainder recipients.

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

Gusto logo
Gusto
Dec 2, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Write a function that distributes an integer budget total among recipients. A map gives the nonnegative integer amount owed to each recipient. Every allocation must be an integer, must not exceed the recipient's amount owed, and must be as even as possible.

For this exercise, interpret evenness as max-min fairness: if one recipient receives at least two more units than another, the lower-paid recipient must already have received their full amount owed. Use as much of the budget as the total debt allows. If several unsatisfied recipients can receive the final leftover units, any such assignment is acceptable.

For example, with amounts owed {a: 10, b: 10, c: 10, d: 10}, a budget of 40 gives ten to each recipient, while 20 gives five to each. With amounts owed {a: 2, b: 10, c: 10, d: 10}, budget 21 can give {a: 2, b: 6, c: 7, d: 6}, and budget 22 can give {a: 2, b: 7, c: 7, d: 6}. Other choices of who receives the extra unit are equally valid.

Constraints and Clarifications

Assume total >= 0 and all amounts owed are nonnegative integers. The map has unique recipient identifiers. If total funds exceed all debt, leave the excess unallocated. These domain and surplus policies are exercise assumptions to confirm before coding.

Clarifying Questions Guidance

  • May any eligible recipient receive a remainder unit, or is a business rule required for repeated allocations?
  • Should recipients owed zero remain in the returned map with zero allocated?
  • How should the caller receive any unused budget when total funds exceed the debt?

What a Strong Answer Covers Guidance

  • Integer allocation, debt caps, budget conservation, and the stated fairness rule
  • A complete function and a trace covering recipients with unequal debt limits
  • Complexity and behavior for zero funds, zero debt, and excess budget
  • The distinction between one valid implementation choice and a required tie policy

Follow-up Questions Guidance

  • How would you make remainder assignments fair over many repeated distributions?
  • How would the algorithm change if recipients had different priority weights?