EasyArraySimulationUpdated Sep 2026

2303. Calculate Amount Paid in Taxes

Asked at Snowflake

Difficulty

Easy

Time

O(b)

Space

O(1)

Pattern

Array

Companies

1

Problem

You are given a list of tax brackets and an amount of income. Each bracket is a pair [upper, percent]. The brackets are sorted by upper, and each upper is larger than the one before it.

Tax is progressive: each slice of your income is taxed at its own rate. The first upper_0 dollars are taxed at percent_0. The next upper_1 - upper_0 dollars are taxed at percent_1, and so on. So a bracket's rate only applies to the money that falls inside that bracket's range, not to your whole income.

Return the total tax you owe as a floating-point number. An answer within 10^-5 of the exact value is accepted.

Income is split into slices

income slices3 @ 50%4 @ 10%3 @ 25%Each slice is the part of income that falls inside onebracket.
With income 10 and brackets [[3,50],[7,10],[12,25]], the money is cut into 3 + 4 + 3 dollars, each slice taxed at its own rate.

Constraints

  • ●1 <= brackets.length <= 100
  • ●1 <= upper_i <= 1000
  • ●0 <= percent_i <= 100
  • ●0 <= income <= 1000
  • ●upper_i is sorted in ascending order
  • ●All values of upper_i are unique
  • ●The upper bound of the last bracket is greater than or equal to income

Examples

Example 1
Inputbrackets = [[3,50],[7,10],[12,25]], income = 10
Output2.65000
WhyThe first 3 dollars are taxed at 50%, giving 1.50. The next 4 dollars (from 3 up to 7) are taxed at 10%, giving 0.40. Only 3 dollars are left (from 7 up to 10), taxed at 25%, giving 0.75. Total = 1.50 + 0.40 + 0.75 = 2.65.
Example 2
Inputbrackets = [[1,0],[4,25],[5,50]], income = 2
Output0.25000
WhyThe first dollar is taxed at 0%, giving 0. The next dollar (from 1 up to 2) is taxed at 25%, giving 0.25. Income stops before the third bracket, so the 50% rate never applies. Total = 0.25.
Example 3
Inputbrackets = [[2,50]], income = 0
Output0.00000
WhyThere is no income, so every bracket contributes 0 and the total tax is 0.

Asked At

1 companies
CompanyDifficulty
SnowflakeEasyView all Snowflake questions →

How to Think About It

1

Bracket i covers income from the previous upper bound (0 for the first) up to upper_i.

2

The taxable amount in a bracket is min(income, upper_i) - prev, clamped at 0. Multiply by percent_i / 100 and add it up.

3

Stop as soon as income <= upper_i — higher brackets do not apply.

4

Walkthrough: brackets [[3,50],[7,10],[12,25]], income 10: 3 * 0.50 + 4 * 0.10 + 3 * 0.25 = 1.5 + 0.4 + 0.75 = 2.65.

5

The result is a floating-point number; answers within 10^-5 are accepted.

Optimal Approach

Step 1: tax = 0, prev = 0.
Step 2: For each (upper, percent):
taxable = min(income, upper) - prev
If taxable <= 0, stop.
tax += taxable * percent / 100
prev = upper.
Step 3: Return tax.

Time: O(b). Space: O(1).

Walk brackets and add each slice

prev = 0, tax = 0taxable = min(income, upper) - prevtax += taxable * percent / 100prev = upperOne pass over the brackets, stopping when taxable is 0or less.bracket walkuppertaxableratetaxb1b2b33350%1.507410%0.4012325%0.75Total after all rows is 1.50 + 0.40 + 0.75 = 2.65.taxable = min(income, upper) - prevtax += taxable * percent / 100
Keep the previous upper bound, take the overlap with income, multiply by the rate, then move on.
ComplexityTime O(b)Space O(1)b is the number of brackets; we scan each bracket once and keep only a running total and the previous upper bound.

What Trips People Up in Real Interviews

1

Taxing the whole income at the highest applicable rate. Progressive tax applies each rate only to its own slice.

2

Integer division in C++/Java (percent / 100 becomes 0). Divide as a double.

3

Continuing past the bracket that contains the income and adding negative amounts.

4

Forgetting that the first bracket starts at 0.

Solution Code

O(b) time
def calculateTax(brackets, income):
    tax = 0.0
    prev = 0
    for upper, percent in brackets:
        taxable = min(income, upper) - prev
        if taxable <= 0:
            break
        tax += taxable * percent / 100
        prev = upper
    return tax

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Calculate Amount Paid in Taxes problem?

Calculate Amount Paid in Taxes gives you progressive tax brackets `[upper, percent]` sorted by upper bound, and an income. Each slice of income between consecutive upper bounds is taxed at that bracket's rate. Return the total tax. It checks that you can walk ranges carefully without off-by-one errors.

How do you solve Calculate Amount Paid in Taxes?

The optimal approach runs in O(b) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Calculate Amount Paid in Taxes?

Calculate Amount Paid in Taxes is asked at Snowflake. It is a easy difficulty problem.

What are common mistakes on Calculate Amount Paid in Taxes?
  • Taxing the whole income at the highest applicable rate. Progressive tax applies each rate only to its own slice.
  • Integer division in C++/Java (`percent / 100` becomes 0). Divide as a double.
  • Continuing past the bracket that contains the income and adding negative amounts.
  • Forgetting that the first bracket starts at 0.