KatInterview

Adding Trip-Level Limits to an Expense Policy Checker

System DesignModel answer
MediumModel answerSystem Designsystem-designrules-engineaggregation

Last updated

Extend a per-expense policy engine so it also enforces trip-wide spending caps, and redesign its output to report both line-item and trip-level violations with clear explanations.

You have already built a policy engine for an expense-reporting product. Its function evaluateRules takes a list of submitted expenses and checks each one on its own against a set of configurable rules, such as a per-item cap. Many companies use this engine through a shared expenses API, and each company can define its own rules.

Now extend the engine to enforce trip-level rules, which depend on the combined spending of all expenses that share a trip:

  1. The total of all expenses on a trip must not exceed $2000.
  2. The total of meal expenses on a trip must not exceed $200.

In the sample data, trip "002" breaks both rules.

Describe how you would:

  • Change the design and the return type of evaluateRules so the result contains trip-level violations next to the existing per-expense violations.
  • Group and aggregate the expenses, and decide in what order the checks run.
  • Make an aggregate violation understandable to the employee and the approver, given that no single expense is the culprit.
  • Keep the evaluation efficient, both for large batches and for single expense edits.
  • Expose the new results through the shared, multi-company expenses API without breaking existing clients.

State your assumptions (currency, missing trip IDs, boundary behavior) and explain the trade-offs. Include a short code sketch of the extended evaluation.

Hints

Hint 1

Introduce a rule scope (expense vs. trip) and treat the new limits as configuration: an optional filter, an aggregate function, a comparison and a threshold. Then you do not need a new code path per rule.

Hint 2

Group expenses by trip once with a hash map and share that grouping across all trip rules. Do money math in integer cents, and decide what 'cannot exceed' means at exactly the limit.

Hint 3

A trip violation needs more than a rule name: include observed total, limit, overage and the contributing expense IDs. For production, think about incremental updates per trip, concurrent submissions, and versioning the changed response shape.