KatInterview

Build a Policy Engine for Expense Validation

Data Structure DesignModel answer
MediumModel answerData Structure Designdata-structuresparsingvalidation

Last updated

Create a rule evaluation system that checks expense submissions against JSON-defined policies without deploying new code.

Problem

You're building the approval workflow for a corporate expense management platform. Business rules like "Any hotel booking over $300 needs manager approval" or "International travel requires supporting documentation" should be configurable by administrators through a UI, not hardcoded.

Your job is to design and implement a system that reads policy definitions from JSON and evaluates them against incoming expense records.

What You Need to Build

1. Policy Representation

Define a JSON format for expressing rules. It should support:

  • Comparisons on expense fields: equality, numeric ordering, set membership, substring matching
  • Logical composition: combining multiple checks with AND, OR, and NOT
  • Outcome specification: what happens when a rule matches (block the expense, flag for review, require additional approval)

2. Evaluation Engine

Implement the evaluation logic. Given an expense object (as a dictionary) and a compiled policy, determine:

  • Does this expense violate the policy?
  • If so, which specific conditions failed?
  • What message should the user see?

Your evaluator should handle:

  • Fields that are missing from the expense data
  • Type mismatches (comparing a string to a number)
  • Deeply nested logical expressions

3. Safety and Correctness

The system must be safe to run in production:

  • Reject malformed or malicious policy JSON at upload time, not at evaluation time
  • Prevent unbounded recursion or memory use
  • Never execute arbitrary code from policy definitions
  • Handle ambiguous cases (missing data) explicitly rather than making assumptions

Example Policy

{
  "id": "travel_advance_booking",
  "description": "Flights booked less than a week out need director sign-off",
  "condition": {
    "op": "AND",
    "clauses": [
      {"field": "expense_type", "predicate": "EQ", "target": "AIRFARE"},
      {"field": "days_until_travel", "predicate": "LT", "target": 7}
    ]
  },
  "outcome": {
    "result": "ESCALATE",
    "escalation_level": "DIRECTOR",
    "reason": "Last-minute flight bookings require director approval per travel policy."
  }
}

An expense with {"expense_type": "AIRFARE", "days_until_travel": 3, "amount": 450} would match this rule and trigger escalation.

What to Cover

  • How you validate policy JSON before storing it
  • How you structure the in-memory representation of a compiled rule
  • How you handle cases where required fields are absent or have unexpected types
  • How you would test individual operators, complex nested conditions, and schema constraints
  • Trade-offs in your design: performance, expressiveness, debuggability

Hints

Hint 1

Think about how to represent boolean logic as a tree where leaves are comparisons and internal nodes are AND/OR/NOT.

Hint 2

Decide what happens when a field is missing: should missing data cause the rule to fail, pass, or return 'uncertain'?

Hint 3

Validate everything you can when the policy is saved, so runtime evaluation only has to worry about bad expense data.