Calculate Amount Paid in Taxes
Asked at Snowflake
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.
Asked At
| 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).
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
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 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 is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.
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.