Compute Progressive Income Tax in Cents from Bracket Limits
SnowflakeSoftware Engineer · Technical ScreenLast updated
Given progressive tax brackets as upper limits with percentage rates and an income in whole dollars, return the total tax owed in cents. It tests careful handling of bracket boundaries, partly filled brackets and exact integer arithmetic instead of floating point.
Income is taxed in brackets. Each bracket has an upper limit and a tax rate: the part of the income up to the first limit is taxed at the first rate, the part between the first and second limits at the second rate, and so on. Given the brackets and an income, return the tax owed.
Function Signature
def tax_in_cents(brackets: list[list[int]], income: int) -> int:
Rules
brackets[i] = [upper, percent], and the brackets are sorted by strictly increasingupper.- Bracket
0covers income from 0 up tobrackets[0][0]. Fori >= 1, bracketicovers the income abovebrackets[i - 1][0]and up tobrackets[i][0]. - The part of the income that falls in bracket
iis taxed atbrackets[i][1]percent. - The income and the limits are whole dollars. Return the total tax in cents, that is the tax in dollars multiplied by 100, which is always a whole number under these inputs.
Constraints
1 <= len(brackets) <= 1001 <= upper <= 10^6for every bracket, strictly increasing0 <= percent <= 100for every bracket0 <= income <= 10^6, andincomeis at most the last bracket'supper.
Examples
Example 1
Input: brackets = [[10000, 0], [40000, 10], [100000, 20]], income = 55000
Output: 600000
The first 10,000 dollars are untaxed, the next 30,000 are taxed at 10 percent (3,000 dollars), and the remaining 15,000 at 20 percent (3,000 dollars). The total is 6,000 dollars, or 600,000 cents.
Example 2
Input: brackets = [[1, 0], [4, 25], [5, 50]], income = 2
Output: 25
One dollar is taxed at 0 percent and one dollar at 25 percent, which is 25 cents. The last bracket is never reached.
Example 3
Input: brackets = [[2, 50]], income = 0
Output: 0
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.