Design an Extensible Expense Rule Engine
RipplingSoftware Engineer · Technical ScreenLast updated
Design an extensible expense rule engine that reports every applicable violation for each expense. The answer separates rule implementations from the evaluation loop, defines deterministic structured results and money handling, and covers boundary tests, malformed data, rule versioning, aggregate-rule extensions, and OOP trade-offs.
Design an Extensible Expense Rule Engine
You receive expenses with fields such as expense_id, item_id, expense_type, amount_in_usd, seller_type, and seller_name. Design and implement the core of evaluate_rules(rules, expenses), which evaluates every configured rule against every expense and returns every violation.
The initial rules are:
- Any expense above 175 violates the total-expense limit.
- An expense from a restaurant seller above 45 violates the restaurant limit.
- Any expense whose type is entertainment violates the prohibited-type rule.
For example:
Expense: {expense_id: "1", expense_type: "Food", amount_in_usd: 250, seller_type: "restaurant"}
Violations:
- Total expense should not be greater than 175
- Restaurant expense should not be greater than 45
New rule types should be addable without modifying the central evaluation loop. Explain your interfaces, result model, ordering, error policy, testing strategy, and complexity.
Clarifying Questions to Ask
- Are monetary values represented as integer cents or exact decimals?
- Should malformed expenses stop the batch or produce a validation violation?
- Must violations preserve expense order and rule-configuration order?
- Can rules depend only on one expense, or will aggregate rules be added later?
Follow-up questions
- How would you add a daily aggregate limit that depends on multiple expenses?
- How would rule versions be audited when policy changes?
- When would a data-driven rule specification be preferable to one class per rule?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.