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
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
brackets = [[3,50],[7,10],[12,25]], income = 102.65000brackets = [[1,0],[4,25],[5,50]], income = 20.25000brackets = [[2,50]], income = 00.00000Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Snowflake | Easy | View all Snowflake questions → |
How to Think About It
Bracket i covers income from the previous upper bound (0 for the first) up to upper_i.
The taxable amount in a bracket is min(income, upper_i) - prev, clamped at 0. Multiply by percent_i / 100 and add it up.
Stop as soon as income <= upper_i — higher brackets do not apply.
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.
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
What Trips People Up in Real Interviews
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.
Solution Code
O(b) timedef 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 taxFrequently 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.