KatInterview

Transaction Policy Engine Design and Implementation

Data Structure DesignModel answer
MediumModel answerData Structure Designrule-enginepolicy-evaluationsystem-design

Last updated

Build a configurable rule engine that evaluates financial transactions against spending policies and returns approval decisions in real-time.

Problem

You need to build a system component that checks whether financial transactions comply with configurable spending policies. The engine must evaluate each transaction against a collection of policies and decide whether to approve or reject it.

Context

Your component receives transaction objects containing:

  • transaction_id: unique identifier
  • user_id: who initiated the transaction
  • amount: monetary value
  • currency: e.g., USD, EUR
  • category: type of purchase (Travel, Software, etc.)
  • merchant_name: where the purchase is made

Policies consist of conditions like "amount exceeds 500" or "category equals Travel", along with an action (permit or block).

Your Task

Design the data structures and logic for this engine. Specifically:

  1. Create a Rule class that represents a single policy with its conditions and action
  2. Implement evaluate_transaction(transaction, rules) that returns either "APPROVED" or "DENIED"
  3. Handle conflicts: when multiple rules match, define clear precedence rules
  4. Enable dynamic updates: policies should be changeable without code deployment

Design Constraints

  • Evaluation latency must stay under 100ms
  • Rules should be serializable (storable in databases or caches)
  • Never use eval() or execute arbitrary code from rule definitions
  • Missing transaction fields should not crash the evaluator

Example

txn = {"amount": 600, "category": "Travel", "user_id": "u123"}
policies = [
    Rule("amount < 500", action="DENY", priority=0),
    Rule("category == Travel", action="DENY", priority=0)
]
result = evaluate_transaction(txn, policies)  # Returns "DENIED"

What to Cover

Explain your design choices for:

  • How you represent conditions and operators
  • How you resolve conflicts between overlapping rules
  • How you ensure the system is fast enough for production
  • What edge cases you handle

Hints

Hint 1

Think about rules as data, not code—how would you serialize a condition into JSON and parse it back safely?

Hint 2

When two rules match the same transaction but disagree, what tie-breaking strategy makes sense for financial controls?

Hint 3

Consider pre-processing rules once when they load, rather than re-sorting on every transaction evaluation