KatInterview

Policy-Driven Spending Report Checker

Data Structure DesignAuto-graded · 14 test cases
HardAuto-gradedData Structure Designobject-oriented designopen-closed principledata aggregation

Last updated

Tests whether a candidate can implement a multi-scope rule evaluation pipeline that collects all violations across item-level, aggregate, and relational policy checks.

Problem Statement

Financial compliance teams need automated tools that can scan submitted spending reports and flag every policy breach — not just the first one found. Your task is to build a rule-based spending report checker that applies a configurable set of policies to a list of expense entries and returns all detected violations.

Data Format

Each expense entry is a dictionary with these keys:

KeyTypeDescription
idstrUnique identifier for the entry
categorystrSpending category, e.g. "MEALS", "AIRFARE"
amountfloatPositive amount with up to 2 decimal places
expense_datestrISO date string, e.g. "2025-03-01"
has_receiptboolWhether a receipt was attached

Each policy rule is a dictionary with a "type" key and type-specific parameters (see below). An optional "rule_name" key overrides the default name used in violation output.

Rule Types

Your checker must support exactly three rule types:

receipt_required Parameters: categories (list of strings), threshold (float). Any entry whose category is in categories and whose amount is strictly greater than threshold must have has_receipt == True. Each offending entry produces its own violation. Default rule_name: "ReceiptRequiredRule"

daily_category_cap Parameters: category (string), daily_limit (float). The combined total of all entries matching category on a single date must not be strictly greater than daily_limit. When a date's total exceeds the cap, produce one violation listing every entry from that date (in input order). Process dates in ascending sorted order. Default rule_name: "DailyCategoryCapRule"

mutual_exclusion Parameters: category_a (string), category_b (string). Entries of category_a and entries of category_b must not share the same expense_date. Each conflicting date produces one violation whose item_ids lists all category_a entries for that date (in input order) followed by all category_b entries for that date (in input order). Process conflict dates in ascending sorted order. Default rule_name: "MutualExclusionRule"

An unknown type value must raise a ValueError.

Output Format

Return a flat list of violation dictionaries, one per detected breach, in rule-declaration order. Within a single rule, violations appear in the order described above for that rule type. Each violation dictionary has exactly these keys:

{
    "rule_name": str,   # default or overridden name
    "message":  str,    # human-readable description
    "item_ids": list    # IDs of the offending entries; empty list if report-wide
}

The message field for each rule must follow this exact format:

  • receipt_required: "{category} expense exceeds {threshold} and requires a receipt"
  • daily_category_cap: "{category} expenses on {date} total {total:g}, exceeding daily limit {limit:g}"
  • mutual_exclusion: "{category_a} and {category_b} expenses cannot both appear on {date}"

Design Note

The checking logic for each rule type is completely self-contained. Processing one rule must never affect another, and a breach found by one rule must not prevent subsequent rules from running. Every applicable violation across all rules must appear in the output.

Constraints

  • The report may contain up to 10^4 expense entries.
  • All amount values are positive floats with up to 2 decimal places.
  • Threshold and cap comparisons are strictly greater than (>); values exactly equal to the threshold or cap are not violations.
  • Dates are ISO-format strings that sort lexicographically in calendar order.
  • The input entry list is not modified by your function.

Examples

Example 1

Input: check_spending_report([{'id': 'e1', 'category': 'MEALS', 'amount': 80, 'expense_date': '2025-03-01', 'has_receipt': False}, {'id': 'e2', 'category': 'MEALS', 'amount': 50, 'expense_date': '2025-03-01', 'has_receipt': True}, {'id': 'e3', 'category': 'CAR_RENTAL', 'amount': 120, 'expense_date': '2025-03-01', 'has_receipt': True}, {'id': 'e4', 'category': 'MILEAGE', 'amount': 45, 'expense_date': '2025-03-01', 'has_receipt': True}], [{'type': 'receipt_required', 'threshold': 75, 'categories': ['MEALS']}, {'type': 'daily_category_cap', 'category': 'MEALS', 'daily_limit': 100}, {'type': 'mutual_exclusion', 'category_a': 'CAR_RENTAL', 'category_b': 'MILEAGE'}])

Output: [{'rule_name': 'ReceiptRequiredRule', 'message': 'MEALS expense exceeds 75 and requires a receipt', 'item_ids': ['e1']}, {'rule_name': 'DailyCategoryCapRule', 'message': 'MEALS expenses on 2025-03-01 total 130, exceeding daily limit 100', 'item_ids': ['e1', 'e2']}, {'rule_name': 'MutualExclusionRule', 'message': 'CAR_RENTAL and MILEAGE expenses cannot both appear on 2025-03-01', 'item_ids': ['e3', 'e4']}]

Hints

Hint 1

For each rule type, think about what grouping structure makes the check efficient — receipt rules need no grouping, cap rules need entries bucketed by date, and exclusion rules need two separate date-to-entries maps that you then intersect.

Hint 2

All three rule types share the same outer contract: iterate over rules, dispatch on type, collect results. A dictionary mapping type strings to handler functions keeps the dispatch clean and makes it easy to add new rule types later without touching the main loop.

Hint 3

Watch the ordering requirements carefully: daily cap and mutual exclusion violations must be emitted in ascending date order (sort the date keys before iterating), and within a mutual exclusion violation the category_a items come before category_b items.