Design Extensible Interview Coding Systems
RipplingSoftware Engineer · OnsiteLast updated
This problem tests your ability to design an extensible rule-evaluation engine using object-oriented principles, specifically focusing on decoupling evaluation logic from business requirements.
The Problem: Extensible Rule Engine
You are tasked with building a rule engine for a payroll processing system. The system needs to validate employee pay data based on various conditions. As the system grows, the company will frequently add new types of validation rules (e.g., minimum wage checks, overtime limits, regional tax compliance).
Design a system that allows for:
- Registering new validation rules without modifying existing engine code (Open/Closed Principle).
- Executing a suite of rules against an
Employeeobject. - Returning a list of all validation errors found.
Requirements
- Define an
Employeeclass with relevant attributes (e.g.,id,salary,hours_worked,state). - Implement a
Ruleinterface or base class. - Implement a
RuleEngineclass that accepts a collection of rules and an employee. - The engine should return a list of
ValidationErrorobjects.
Example
# Expected usage pattern
employee = Employee(id=1, salary=50000, hours_worked=45, state="CA")
engine = RuleEngine()
engine.add_rule(OvertimeLimitRule(max_hours=40))
engine.add_rule(MinimumSalaryRule(min_salary=30000))
errors = engine.validate(employee)
# errors will contain violations if the employee exceeds 40 hours or earns < 30k
Constraints
- The engine must handle arbitrary quantities of rules.
- Rules might require external parameters (like
max_hours) at the time of creation. - Consider how you would handle rule prioritization (e.g., critical rules must run first).
Hints
Hint 1
Use the Strategy pattern to encapsulate the validation logic for each specific rule.
Hint 2
Consider utilizing a Composite pattern if you need to group multiple rules into a single logical validation block.
Hint 3
To handle prioritization, store rules in a sorted structure or include a 'weight' attribute in the Rule interface.
Follow-up questions
- How would you modify your design if some rules require data from an external database or API call?
- If the number of rules grows to thousands, how would you optimize the performance of the `RuleEngine.validate()` method?
- How would you implement a 'short-circuit' mechanism where validation stops immediately upon encountering a critical rule violation?
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.