Problem · Array

Tax Calculation with Year and Filing Rules

Learn this problem
MediumGusto logoGustoFULLTIMEONSITE INTERVIEW

Problem statement

Calculate tax for taxableIncome, expressed in integer cents. Tax rules are supplied in parallel arrays. Rule i applies when ruleYears[i] == year and ruleStatuses[i] equals filingStatus. Exactly one rule matches.

For a matching rule, bracketEnds[i][j] is the exclusive upper income bound of marginal bracket j, and ratesBps[i][j] is its rate in basis points, where 100 basis points equal one percent. Brackets start at 0, are contiguous, and the final end covers taxableIncome. For each bracket, round that bracket's tax down to a whole cent, then sum the bracket taxes.

If customRateBps is nonnegative, it overrides the matching marginal rule. Return floor(taxableIncome * customRateBps / 10000). A value of -1 means no custom rate.

Function

calculateTax(taxableIncome: long, year: int, filingStatus: String, ruleYears: int[], ruleStatuses: String[], bracketEnds: long[][], ratesBps: int[][], customRateBps: int) → long

Examples

Example 1

taxableIncome = 100000year = 2026filingStatus = "single"ruleYears = [2026]ruleStatuses = ["single"]bracketEnds = [[50000,100000]]ratesBps = [[1000,2000]]customRateBps = -1return = 15000

The first 50000 cents are taxed at 10% for 5000 cents, and the next 50000 at 20% for 10000 cents.

Example 2

taxableIncome = 100000year = 2026filingStatus = "single"ruleYears = [2026]ruleStatuses = ["single"]bracketEnds = [[50000,100000]]ratesBps = [[1000,2000]]customRateBps = 1500return = 15000

The custom 15% rate overrides the marginal brackets.

Example 3

taxableIncome = 60000year = 2025filingStatus = "joint"ruleYears = [2025,2025]ruleStatuses = ["single","joint"]bracketEnds = [[30000,100000],[40000,100000]]ratesBps = [[1000,2000],[500,1000]]customRateBps = -1return = 4000

The joint rule applies: 40000 cents at 5% plus 20000 cents at 10%.

Constraints

  • 0 <= taxableIncome <= 10^12.
  • 1 <= ruleYears.length == ruleStatuses.length == bracketEnds.length == ratesBps.length <= 1000.
  • Exactly one rule matches year and filingStatus.
  • Each rule has 1 to 50 strictly increasing positive bracket ends, and its final end is at least taxableIncome.
  • Each rate is between 0 and 10000 basis points.
  • customRateBps == -1 or 0 <= customRateBps <= 10000.

More Gusto problems

drafts saved locally
public long calculateTax(long taxableIncome, int year, String filingStatus, int[] ruleYears, String[] ruleStatuses, long[][] bracketEnds, int[][] ratesBps, int customRateBps) {
  // Write your code here.
}
taxableIncome100000
year2026
filingStatus"single"
ruleYears[2026]
ruleStatuses["single"]
bracketEnds[[50000,100000]]
ratesBps[[1000,2000]]
customRateBps-1
expected15000
checking account