Progressive Tax Calculator
Learn this problemProblem statement
Implement a tax calculator for a progressive marginal tax system.
You are given two arrays of equal length:
lowerCutoffs[i]: the lower income cutoff for tax bracketi.rates[i]: the marginal tax rate for tax bracketi, written as a decimal. For example,0.1means10%.
The brackets are sorted by lowerCutoffs in increasing order, and lowerCutoffs[0] = 0.
For bracket i, the taxable range starts at lowerCutoffs[i]. If bracket i + 1 exists, the range ends at lowerCutoffs[i + 1]. Otherwise, the final bracket has no upper limit.
Given an income income, calculate the total tax owed. Each portion of income is taxed only at the marginal rate for the bracket containing that portion.
Function
calculateTax(lowerCutoffs: long[], rates: float[], income: long) → floatExamples
Example 1
lowerCutoffs = [0, 10000, 50000]rates = [0.125, 0.25, 0.5]income = 60001return = 16250.5The first 10000 is taxed at 12.5%, the next 40000 is taxed at 25%, and the final 10001 is taxed at 50%. The total is 1250 + 10000 + 5000.5 = 16250.5.
Example 2
lowerCutoffs = [0, 100]rates = [0.125, 0.25]income = 151return = 25.25The first 100 is taxed at 12.5%, and the remaining 51 is taxed at 25%, for 12.5 + 12.75 = 25.25.
Constraints
1 <= lowerCutoffs.length = rates.length <= 10^5lowerCutoffsis sorted in increasing order.lowerCutoffs[0] = 00 <= rates[i] <= 1income >= 0