Calculate Progressive Income Tax in Cents
Math & Bit ManipulationAuto-graded · 23 test casesLast updated
Determine progressive income tax in cents given bracket thresholds and percentage rates using integer math.
Under a progressive tax structure, income is broken into contiguous bands (brackets), each assessed at a specific percentage rate. The portion of income within the first band is taxed at the first rate, the next portion at the second rate, and so forth.
You are given a list of tax brackets and an overall gross income in whole dollars. Compute and return the total tax owed measured in cents.
Each element in brackets is a pair [upper, percent]:
- The first bracket covers income from
$0up to$brackets[0][0]. - For any bracket index $i \ge 1$, bracket $i$ covers income greater than
$brackets[i - 1][0]up to$brackets[i][0]. - The rate
percentrepresents the percentage tax rate applied to income within that range.
Because $1\text{ dollar} = 100\text{ cents}$, the tax contribution in cents for a slice of $D$ dollars taxed at $P%$ is: $$\text{tax in cents} = D \times \frac{P}{100} \times 100 = D \times P$$
This allows computing the exact tax in cents using integer operations alone.
Constraints
1 <= len(brackets) <= 1001 <= upper <= 10^6for each bracket, strictly increasing (upper[i - 1] < upper[i]).0 <= percent <= 100for each bracket.0 <= income <= 10^6incomeis guaranteed to be less than or equal to theupperboundary of the final bracket.
Examples
Example 1
Input: tax_in_cents([[10000, 0], [40000, 10], [100000, 20]], 55000)
Output: 600000
Example 2
Input: tax_in_cents([[1, 0], [4, 25], [5, 50]], 2)
Output: 25
Example 3
Input: tax_in_cents([[2, 50]], 0)
Output: 0
Hints
Hint 1
Iterate through the brackets while maintaining the lower threshold of the active bracket.
Hint 2
For each bracket, calculate the dollar amount of income that falls inside it: `min(income, upper) - lower_bound`.
Hint 3
Since $1 \text{ dollar} = 100 \text{ cents}$, a dollar amount $D$ taxed at $P\%$ yields $D \times P$ cents directly, avoiding floating-point division.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem asks us to evaluate a progressive tax system over income brackets defined by increasing upper dollar limits.
We can process the brackets sequentially starting from $0:
- Maintain a tracking variable
lower_limit(initially0) for the starting threshold of the current bracket. - For each bracket
[upper_limit, rate]:- If
income <= lower_limit, no further income remains to be taxed, so we can exit early. - Otherwise, determine the income contained in the current bracket:
taxable_dollars = min(income, upper_limit) - lower_limit. - Calculate tax in cents for this segment. Notice that $D \text{ dollars} \times \frac{P}{100} \times 100 \text{ cents/dollar} = D \times P \text{ cents}$. Hence, we add
taxable_dollars * ratedirectly to our running total. - Update
lower_limit = upper_limit.
- If
This avoids floating-point operations altogether, preventing precision errors inherent to floating-point arithmetic in financial calculations.
Complexity
- Time Complexity: $\mathcal{O}(N)$, where $N$ is the number of tax brackets. We iterate through the bracket list at most once.
- Space Complexity: $\mathcal{O}(1)$ auxiliary space as we only store a few integer variables for tracking thresholds and totals.
Python Reference Solution
from typing import List
def tax_in_cents(brackets: List[List[int]], income: int) -> int:
total_cents = 0
lower_limit = 0
for upper_limit, rate in brackets:
if income <= lower_limit:
break
taxable_dollars = min(income, upper_limit) - lower_limit
total_cents += taxable_dollars * rate
lower_limit = upper_limit
return total_cents