KatInterview

Calculate Progressive Income Tax in Cents

Math & Bit ManipulationAuto-graded · 23 test cases
EasyAuto-gradedMath & Bit Manipulationmatharrayssimulation

Last 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 $0 up 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 percent represents 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) <= 100
  • 1 <= upper <= 10^6 for each bracket, strictly increasing (upper[i - 1] < upper[i]).
  • 0 <= percent <= 100 for each bracket.
  • 0 <= income <= 10^6
  • income is guaranteed to be less than or equal to the upper boundary 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.