Compute Tax Using Marginal Brackets
Math & Bit ManipulationAuto-graded · 22 test casesLast updated
Tests understanding of piecewise rate application and interval-based accumulation in a tiered tax system.
You are building a tax calculator that applies rates in tiers. You receive a sorted list of tax brackets and a total income amount. Each bracket is represented as a pair (threshold, rate) where threshold is the lower bound (inclusive) and rate is a decimal (e.g., 0.15 means 15%).
How the system works:
- The first bracket starts at its threshold and extends up to the next bracket's threshold.
- Each subsequent bracket covers the range from its own threshold up to the threshold of the following bracket.
- The final bracket extends indefinitely beyond its threshold.
- Only the portion of income that falls within a bracket is taxed at that bracket's rate.
- Income below the first threshold is not taxed.
Your task is to return the total tax owed.
Constraints:
0 ≤ len(brackets) ≤ 100- Brackets are sorted in ascending order by threshold
0 ≤ threshold ≤ 10^90 ≤ rate ≤ 10 ≤ income ≤ 10^9- All thresholds are unique
- Return the exact floating-point sum; do not round
Examples
Example 1
Input: compute_marginal_tax([[0, 0.1], [10000, 0.2], [50000, 0.3]], 5000)
Output: 500
Example 2
Input: compute_marginal_tax([[0, 0.1], [10000, 0.2], [50000, 0.3]], 25000)
Output: 4000
Example 3
Input: compute_marginal_tax([[0, 0.1], [10000, 0.2], [50000, 0.3]], 100000)
Output: 24000
Example 4
Input: compute_marginal_tax([[1000, 0.1], [5000, 0.15], [20000, 0.25]], 5000)
Output: 400
Hints
Hint 1
Process brackets sequentially; stop as soon as income is below the current threshold.
Hint 2
For each bracket, determine how much income falls within its range by finding the upper boundary (the next bracket's threshold, or infinity for the last bracket).
Hint 3
Accumulate tax by multiplying the taxable slice in each bracket by that bracket's rate.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that a marginal tax system divides income into non-overlapping slices, each taxed at its own rate. We iterate through the brackets in order, computing how much income falls into each tier and multiplying that amount by the tier's rate.
For each bracket at index i:
- Check eligibility: If income is at or below the current threshold, no income reaches this bracket, so we stop.
- Determine the upper boundary: If there is a next bracket, the upper limit is that bracket's threshold. Otherwise, the bracket is unbounded.
- Calculate the taxable slice: Take the minimum of income and the upper boundary, then subtract the current threshold. This gives the portion of income taxed at this rate.
- Accumulate tax: Multiply the slice by the rate and add to the running total.
Income below the first threshold is never taxed because the loop starts at the first bracket's lower bound.
Why It Works
The brackets partition the income range into intervals [t₀, t₁), [t₁, t₂), ..., [tₖ, ∞). For a given income I, we slice it across these intervals. Each slice is independent and taxed at exactly one rate. Summing slice_width × rate across all brackets gives the total tax by definition.
The early break optimization is valid because brackets are sorted: once income doesn't reach a threshold, it won't reach any later threshold either.
Complexity
- Time: O(n), where n is the number of brackets. We visit each bracket at most once and often break early.
- Space: O(1) additional space.
Solution
from typing import List
def compute_marginal_tax(brackets: List[List[float]], income: float) -> float:
tax = 0.0
for i, (lower, rate) in enumerate(brackets):
if income <= lower:
break
if i + 1 < len(brackets):
upper = brackets[i + 1][0]
taxable = min(income, upper) - lower
else:
taxable = income - lower
if taxable > 0:
tax += taxable * rate
return tax