Fair Capped Money Distribution
Updated
Problem statement
You have amount units of money to hand out. A unit is the smallest piece of the currency, such as one cent, and amount is a whole number that is zero or more. You also get an array owed in a fixed order, where owed[i] is the most recipient i may receive. We call this limit the recipient's cap.
Hand out the money in rounds. In each round, go through the recipients in order and give one unit to each recipient who still has less than they are owed. Stop when the money runs out or every recipient is paid in full. Return how much each recipient got, in the original order.
This way, payments are as even as the caps allow. Units left over after the last full round go to the earlier recipients first. No one gets more than they are owed, and exactly min(amount, sum(owed)) units are handed out in total.
Function
distributeMoney(amount: long, owed: long[]) → long[]Examples
Example 1
amount = 40owed = [10,10,10,10]return = [10,10,10,10]The available amount exactly covers every recipient's cap.
Example 2
amount = 10owed = [2,100,100]return = [2,4,4]The first recipient reaches its cap at 2. The remaining units are split evenly between the other two recipients.
Example 3
amount = 5owed = [10,10,10]return = [2,2,1]One complete round gives everyone one unit. The two leftover units go to the first two recipients.
Constraints
0 <= amount <= 10^18.1 <= owed.length <= 100000.0 <= owed[i] <= 10^18.- The sum of all owed amounts fits a signed 64-bit integer.
More Gusto Onsite Interview questions
Similar questions at other companies
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Remove the Fewest Magic BeansThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Aggressive CowsAmazon · PHONE SCREEN
- Unit-Weight Knapsack with Limited QuantitiesGoogle · ONSITE INTERVIEWGoogle interview questions
- Minimize Range After Three ChangesDRW · PHONE SCREENDRW interview questions
- Reduce Array Size to At Most HalfMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Maximize Monsters DefeatedSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions