Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Tax Withholding Rules Function
00:00
5 left

Tax Withholding Rules Function

HardPython

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):
Interviewer

Your question is Tax Withholding Rules Function. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.