Distribute Annual Revenue Across Twelve Months
RilletBackend Engineer · Technical ScreenLast updated
This problem tests logic, integer arithmetic, and edge-case handling by requiring the distribution of a total sum into equal parts while managing remainders without using floating-point numbers.
Problem Statement
Write a function distribute_revenue(total_cents) that takes an annual revenue amount expressed as a total number of cents (a non-negative integer). You must return a list of twelve integers representing the monthly revenue for each month of the year.
The distribution must satisfy these rules:
- The sum of the twelve integers must equal
total_centsexactly. - Each monthly amount must be as close to the average as possible (i.e.,
total_cents // 12or(total_cents // 12) + 1). - Any remainder resulting from the division by 12 must be distributed, one cent per month, starting from the first month (index 0) until the remainder is exhausted.
Function Signature
def distribute_revenue(total_cents: int) -> list[int]:
Examples
Example 1:
Input: total_cents = 1200
Output: [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
Explanation: 1200 divided by 12 is exactly 100 with no remainder.
Example 2:
Input: total_cents = 13
Output: [2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Explanation: 13 / 12 = 1 with a remainder of 1. The first index gets 1 + 1 = 2.
Example 3:
Input: total_cents = 25
Output: [3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
Explanation: 25 / 12 = 2 with a remainder of 1. The first index gets 2 + 1 = 3.
Hints
Hint 1
Use integer division (//) to find the baseline monthly amount that applies to all twelve months.
Hint 2
Use the modulo operator (%) to determine the total number of remainder cents that need to be distributed.
Hint 3
Construct the result list by iterating 12 times, adding the baseline value plus 1 if the current index is less than the remainder value.
Follow-up questions
- How would your implementation change if you needed to distribute the remainder to the *last* months instead of the first?
- If the input was a list of start and end dates for a partial year, how would you calculate the revenue distribution proportionally based on the number of days in each month?
- What are the time and space complexity of your solution, and can it be optimized?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.