Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Tax Withholding Rules Function

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Tax Withholding Rules Function. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Gusto Payroll supports tax configurations that change by filing status, income range, and tax year. Given a paycheck and a dynamic collection of tax rules, calculate the employee's estimated federal withholding for that paycheck.

All monetary values are integer cents. Annualize the current paycheck and pre-tax deductions using pay_periods, select the matching rule with the greatest priority, and calculate tax progressively over the rule's brackets.

Formal Specification

Implement calculate_withholding(gross_pay, pay_periods, pre_tax_deductions, filing_status, rules). gross_pay, pre_tax_deductions, and every rule amount are nonnegative integers in cents. pay_periods is a positive integer. rules is a list of objects with priority, statuses, income_min, income_max, standard_deduction, brackets, and credits.

A rule matches when the filing status is listed in statuses, annual gross income is at least income_min, and it is below income_max, unless income_max is null. Each bracket has an exclusive up_to limit in taxable-income cents, or null for infinity, and an integer rate_bps, where 10,000 basis points equals 100%. Each credit has an amount and optional max_taxable_income. Apply credits only when eligible, never allowing tax below zero. Round each bracket's tax and the final per-paycheck amount to the nearest cent, with halves rounded up.

Constraints

  • 1 <= len(rules) <= 10^4
  • 1 <= len(brackets), len(credits) <= 10^3
  • Rule brackets are sorted, non-overlapping, and cover all nonnegative taxable income
  • Exactly one matching rule has the greatest priority
  • Rates are integers from 0 through 10000 basis points
  • All monetary inputs are nonnegative integer cents

Function Signature

def calculate_withholding(gross_pay, pay_periods, pre_tax_deductions, filing_status, rules):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output