KatInterview

Design a Pluggable Payroll Cost Estimator

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-modelingapi-designmoney-handling

Last updated

Design the types and API that compute an employee's full cost from salary, tax, benefits, and a signed location adjustment, with exact money math, explicit missing-rule handling, and versioned rules.

You are asked to design the classes and the calculation entry point for a service that reports what an employee costs the business. The total is the sum of four parts:

  1. Salary: the base pay stored on the employee record.
  2. Tax: a single flat percentage applied to the base salary.
  3. Benefits: an amount found in a table indexed by the pair (level, title).
  4. Location adjustment: an amount found in a table indexed by location. It may be positive (a premium) or negative (a discount).

Describe the types you would define and how the calculation works. Your answer should cover:

  • Input validation.
  • How money is represented and rounded.
  • What happens when a benefits or location entry is missing.
  • How rule data is versioned.
  • How you would test the design.

The design should be easy to extend (for example, adding a fifth cost component later) and should not rely on a long chain of hard-coded conditionals.

Assumptions

  • Every calculation uses one declared currency.
  • The tax rate and both lookup tables are supplied as configuration. They are not constants buried inside the employee class.
  • Level, title, and location are given as canonical identifiers, not free text.
  • Re-running a calculation after the rules change must still be able to reproduce the original result.

Questions worth raising with the interviewer

  • Can the tax rate depend on the employee or location, or is it truly one flat rate?
  • Should a missing rule raise an error, count as zero, or fall back to a more general rule?
  • Are location adjustments fixed amounts or percentages, and are they annual?
  • What rounding mode and currency precision apply?

Hints

Hint 1

Split the problem into data (employee), rules (rates and lookup tables), and output (a result object). The employee should not know how cost is computed.

Hint 2

Think of each cost part as a small component that takes the employee and a rules snapshot and returns a named, signed money amount. The total is the sum of the components, so adding a new part does not touch existing ones.

Hint 3

Avoid floats. Use a decimal type or integer minor units, define one rounding point, and decide up front whether a missing lookup is an error, a zero, or a fallback. Stamp each result with the rules version it used.