KatInterview

Build a Fee Calculator with Comprehensive Test Coverage

Data Structure DesignModel answer
MediumModel answerData Structure Designtestingvalidationobject-oriented-design

Last updated

Write a service class that computes fees based on business rules, then extend it and write unit tests covering normal cases, boundaries, and errors.

You need to build a fee calculation service from scratch. You'll receive a written specification describing the pricing rules. Your task has three components:

  1. Implement the core pricing logic using straightforward conditional statements and thorough input validation
  2. Extend the implementation with an additional feature after clarifying requirements
  3. Write comprehensive unit tests that verify correct behavior for typical inputs, boundary conditions, and invalid input handling

The service should be a standalone class with no external dependencies. State all assumptions explicitly before coding. Provide clear method signatures and demonstrate your testing approach with representative test cases.

Example specification (your interviewer will provide the actual spec):

Create a ShippingCostService class with a method compute_cost(weight_oz, zone) that returns cost in cents:

  • Base cost is 399 cents
  • Packages under 16 oz are free to ship
  • Each ounce from 16 oz onward adds 25 cents (round partial ounces up)
  • Zone must be 1-8; weight must be 0-128 oz
  • Invalid inputs should raise appropriate exceptions

Extension: Add an optional is_premium parameter. Premium members with packages over 32 oz pay half the per-ounce rate.

Your solution should demonstrate proper type checking, range validation, backward compatibility when extending, and comprehensive test coverage.

Hints

Hint 1

Validate all inputs before performing any calculations—fail fast with clear error messages

Hint 2

Use named constants for all business values so pricing rules can be updated in one place

Hint 3

When extending functionality, use keyword-only arguments with defaults to maintain backward compatibility