This post is completed by 1 user

  • 0
Add to List
Beginner

502. Calculate tax on income as per given tax brackets.

Given a list of tax brackets (different tax on different incomes). Write a program to calculate the tax on given income. 

Tax bracket is a pair consisting of amount and percentage tax on that amount. If amount is null means, it's a highest bracket and percentage tax is mentioned will be applied to all the remaining income. See the example below for more understanding. 

Example:

Tax brackets:
[10000, 0.10]
[20000, 0.20]
[15000, 0.30]
[null, 0.50]

Income: 100,000.
total_tax :
For First 10000, 10% tax, total_tax = 1000
Next 20000, 20% tax, total_tax = 1000 + 4000 = 5000
Next 15000, 30% tax, total_tax = 5000 + 4500 = 9500
For remaining 55000, 50% tax, total_tax = 27500 + 9500 = 37000.

Approach:

  1. Initialize the taxes as 0.
  2. bracket_Index = 0.
  3. while income > 0
    • Get the tax bracket at index = bracket_Index. 
    • Get the tax percentage for the picked tax bracket.
    • If the tax bracket limit is not null then Pick the minimum of (tax bracket limit and income) and multiply it with the tax percentage and add it to the taxes. (why minimum because let's say income is 6000 and tax bracket is 10000. So we need to calculate the tax only on 6000.)  reduce the income by the picked amount and increment the tax_bracket index by 1.
    •   If the tax bracket limit is null then we are at the highest tax bracket, do income * tax percentage.
  4. Return the taxes.

Output: 

Calculated Taxes: 37000.0 for income: 100000