KatInterview

Policy Engine for Flagging Company Card Spending

System DesignModel answer
HardModel answerSystem Designsystem-designrules-enginemulti-tenant

Last updated

Design a multi-tenant engine that checks card transactions and whole trips against configurable policies, with auditable results and an API that can evolve without breaking clients.

Problem

Your company's finance platform serves many customer organizations. You must design a service that reviews corporate card transactions and marks the ones a manager should look at.

Starting point

Transactions arrive as a list of dictionaries in which every key and value is a string. The first policy contains four checks:

  1. A restaurant charge must not exceed $75.
  2. Airfare is not allowed.
  3. Entertainment is not allowed.
  4. No single charge may exceed $250.

Define your own types for rules, then propose the signature and return type of:

evaluateRules(rules: list<Rule>, expenses: list<Expense>) -> ReturnType

The result will be returned by an expenses API shared by many companies.

Design goals

  • Handle 100+ rules chosen per manager or team. New rules must be creatable through an API, and adding them must not break existing clients.
  • State the evaluation semantics: ordering and priority, combinators (AND / OR / NOT), short-circuit versus exhaustive evaluation, and how conflicting rules are resolved.
  • Make results explainable and auditable: a reason per violation, rule metadata, timestamps, and which policy and which actor or system caused it.
  • Cover tenant isolation, authorization, observability, idempotency, deterministic output order, and resilience when part of a batch fails.
  • Define the core models (Expense, Trip, Rule, Policy, RuleResult, EvaluationSummary) and explain how string inputs for amounts, categories, and vendor types are parsed and validated.

Extension: trip-level rules

Add rules that work on groups of expenses:

  • (a) The total for a trip must not exceed $2000.
  • (b) Total meal spending within a trip must not exceed $200.

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

Extend the engine so single-expense and trip-level rules are evaluated together. Describe how you aggregate, what you cache, and how you update results incrementally. Then propose a backward-compatible return type that reports both expense-level and trip-level violations, including entity type, identifiers, violated rule codes, severity, readable messages, machine-readable fields, and pagination.

Clarifications to raise

Discuss, and state assumptions for, the following:

  • Policy scoping (company / team / user) and how overrides and precedence work.
  • Currency conversion, locale, whether tax and tip count toward meal caps, and per-diem versus per-transaction limits.
  • Category taxonomy (expense type, vendor type): who assigns it, and what happens with unknown or wrong labels.
  • Time windows (trip, day, month), receipts, exception and approval flows, notifications.
  • Versioning, gradual rollout, rate limits, and long-term compatibility of the return type.

What to deliver

A structured design: assumptions, data models, rule representation, evaluation semantics, the function signature and return type, the aggregate extension, and the main trade-offs.

Hints

Hint 1

Treat rules as stored data (a small expression tree with a whitelist of fields and operators) rather than code, so new rules need no deploy and can be validated when they are created.

Hint 2

Return one flat, sorted, paginated list of results, each tagged with an entity type and ID. Adding trip-level results then means adding rows, not changing the shape, which keeps old clients working.

Hint 3

For aggregate rules, group parsed expenses by key once, compute each needed sum a single time, and cache per-group running totals so a new or edited expense only adjusts its own group rather than triggering a full recomputation.