KatInterview

Build a Policy-Driven Expense Approval Engine

System DesignModel answer
MediumModel answerSystem Designobject-oriented designextensibilitystrategy pattern

Last updated

Design an extensible rule evaluation system that validates expense reports against configurable business policies and returns a structured approval decision.

Background

Your team is building an internal tool to automate expense approvals. Finance has a growing list of policies — things like "meals over $75 need manager sign-off" or "any international trip requires VP approval" — and they keep adding new ones. Right now every new policy means touching the core evaluation code, which keeps breaking things.

Your job is to design a small but well-structured system that separates each policy from the engine that runs them, so new policies can be added as isolated units of code.

What to Design

An expense report has at minimum these fields:

amount: float
currency: str           # e.g. "USD", "EUR"
category: str           # e.g. "MEALS", "TRAVEL", "SOFTWARE"
date: date
employee_level: int     # e.g. 1–5
vendor_id: str
is_international: bool

Your system needs three things:

1. A Rule abstraction Each rule inspects a report and returns a structured result: did it pass, does it need review, or should it be rejected outright? The result should carry a human-readable reason.

2. An Evaluator class It holds a list of rules and runs them all against a given report. It produces a single final decision: APPROVED, NEEDS_REVIEW, or REJECTED. Define clearly how multiple rule results get folded into one outcome.

3. A registration mechanism New rule types should be addable without modifying the Evaluator. Concrete rule instances should be constructable from configuration (e.g., a rule type name plus parameters).

Example Interface

evaluator = Evaluator()
evaluator.add_rule(build_rule("category_cap", "meal-cap", category="MEALS", cap=75.0))
evaluator.add_rule(build_rule("approval_threshold", "large-spend", limit=500.0, approver="MANAGER"))

decision = evaluator.evaluate_report(report, context)
# decision.status -> "NEEDS_REVIEW"
# decision.reasons -> ["Over $500", ...]
# decision.required_approvers -> {"MANAGER"}

Requirements and Constraints

  • Rules may inspect any combination of fields on the report.
  • When multiple rules fire, define and justify your aggregation strategy.
  • Some rules need external data (e.g., a live currency conversion rate, a vendor risk list). Show how rules access that data without coupling to a specific service.
  • Address these edge cases explicitly:
    • No rules are registered
    • A rule throws an exception (e.g., the FX service is unavailable)
    • Multiple rules each require a different approver
    • Negative or zero amounts (refunds, data errors)

A written walkthrough plus working Python code is expected. Discuss the design trade-offs you made.

Hints

Hint 1

Think about what the Strategy pattern buys you here: if every rule implements the same interface, the Evaluator never needs to know what any specific rule does. What should that interface look like?

Hint 2

For aggregation, model outcomes as an ordered severity scale (pass < flag < fail). The final decision is the maximum severity across all rules — this makes the result order-independent and easy to reason about.

Hint 3

Rules that need external data (FX rates, vendor risk lists) should not call services directly. Consider passing an injected context object into each rule's evaluate method, so rules stay pure and tests can pass in fakes.