Calculate Amount Paid in Taxes - Problem

Calculate Your Tax Bill

Imagine you're a tax accountant working for a financial consulting firm. Your job is to calculate exactly how much tax someone owes based on a progressive tax system.

You are given a brackets array where each element [upper_bound, tax_rate] represents a tax bracket. The brackets are sorted by upper bound, and taxes are calculated in a tiered system:

  • 💰 The first upper₀ dollars are taxed at percent₀%
  • 💰 The next (upper₁ - upper₀) dollars are taxed at percent₁%
  • 💰 The next (upper₂ - upper₁) dollars are taxed at percent₂%
  • 💰 And so on...

Goal: Given someone's total income, calculate the total amount of tax they need to pay.

Example: If brackets are [[3,50], [7,10], [12,25]] and income is 10:
• First $3 taxed at 50% = $1.50
• Next $(7-3) = $4 taxed at 10% = $0.40
• Next $(10-7) = $3 taxed at 25% = $0.75
• Total tax = $1.50 + $0.40 + $0.75 = $2.65

Input & Output

example_1.py — Basic Tax Calculation
$ Input: brackets = [[3,50],[7,10],[12,25]], income = 10
Output: 2.65000
💡 Note: First $3 taxed at 50% = $1.50, next $4 (7-3) taxed at 10% = $0.40, remaining $3 (10-7) taxed at 25% = $0.75. Total = $2.65
example_2.py — Income Fits in First Bracket
$ Input: brackets = [[1,0],[4,25],[5,50]], income = 2
Output: 0.25000
💡 Note: First $1 taxed at 0% = $0.00, next $1 (2-1) taxed at 25% = $0.25. Total = $0.25
example_3.py — High Income Spanning All Brackets
$ Input: brackets = [[2,10],[5,20],[10,30]], income = 15
Output: 4.00000
💡 Note: First $2 at 10% = $0.20, next $3 at 20% = $0.60, next $5 at 30% = $1.50, remaining $5 at 30% = $1.50. Total = $3.80

Visualization

Tap to expand
$350% → $1.50$410% → $0.40$325% → $0.75Total Tax: $1.50 + $0.40 + $0.75 = $2.65Income $10 Flowing Through Tax Brackets
Understanding the Visualization
1
Setup
Income of $10 needs to flow through 3 tax brackets with rates 50%, 10%, and 25%
2
First Bracket
$3 flows through first bracket at 50% rate → Tax: $1.50
3
Second Bracket
Remaining $7, but only $4 fits in this bracket at 10% → Tax: $0.40
4
Third Bracket
Final $3 flows through last bracket at 25% rate → Tax: $0.75
Key Takeaway
🎯 Key Insight: Process income sequentially through each tax bracket, calculating tax only on the portion that fits within each bracket's range. This simulates the real-world progressive tax system efficiently in O(n) time.

Time & Space Complexity

Time Complexity
⏱️
O(n)

We iterate through each tax bracket once, where n is the number of brackets

n
2n
Linear Growth
Space Complexity
O(1)

We only use a constant amount of extra space for variables

n
2n
Linear Space

Constraints

  • 1 ≤ brackets.length ≤ 100
  • 1 ≤ upperi ≤ 1000
  • 0 ≤ percenti ≤ 100
  • 0 ≤ income ≤ 1000
  • brackets is sorted in ascending order by upperi
  • All the values of upperi are unique
Asked in
Meta 15 Amazon 12 Google 8 Microsoft 6
26.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen