KatInterview

Compute Tax Using Marginal Brackets

Math & Bit ManipulationAuto-graded · 22 test cases
EasyAuto-gradedMath & Bit Manipulationsimulationmathiteration

Last 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^9
  • 0 ≤ rate ≤ 1
  • 0 ≤ 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.