Problem · Array

Progressive Tax Calculator

Learn this problem
EasyInteractive BrokersFULLTIMEOA

Problem 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 bracket i.
  • rates[i]: the marginal tax rate for tax bracket i, written as a decimal. For example, 0.1 means 10%.

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) → float

Examples

Example 1

lowerCutoffs = [0, 10000, 50000]rates = [0.125, 0.25, 0.5]income = 60001return = 16250.5

The 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.25

The 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^5
  • lowerCutoffs is sorted in increasing order.
  • lowerCutoffs[0] = 0
  • 0 <= rates[i] <= 1
  • income >= 0
drafts saved locally
public float calculateTax(long[] lowerCutoffs, float[] rates, long income) {
  // write your code here
}
lowerCutoffs[0, 10000, 50000]
rates[0.125, 0.25, 0.5]
income60001
expected16250.5
checking account