Design and implement a simple expression evaluator for financial rules

Last updated: August 6, 2025

Quick Overview

Build an evaluator for a simple DSL used to define financial rules. Support arithmetic operations, comparisons, logical operators, and variable references. Example: 'credit_score >= 600 AND debt_to_income < 0.4 AND loan_amount <= max_approved'.

Affirm
Coding & Algorithms
Software Engineer
Affirm
August 6, 2025
Software Engineer
Onsite Coding Round
Coding & Algorithms
Hard

6

8

4,026 solved


Build an evaluator for a simple DSL used to define financial rules. Support arithmetic operations, comparisons, logical operators, and variable references. Example: 'credit_score >= 600 AND debt_to_income < 0.4 AND loan_amount <= max_approved'.

Affirm's credit decisioning and eligibility rules are configured as expressions that non-engineers (risk analysts) can modify. This problem tests your ability to build a parser/evaluator, which is relevant to Affirm's rules engine infrastructure.

What the Interviewer Expects
  • Parse the expression into an abstract syntax tree (AST)
  • Evaluate the AST against a context dictionary of variable values
  • Handle operator precedence correctly (AND/OR, comparisons, arithmetic)
  • Return clear error messages for invalid expressions or missing variables
  • Write clean, extensible code that makes adding new operators easy
Key Topics to Cover
Parsing and tokenization
Abstract syntax trees
Recursive descent parsing
Operator precedence
Domain-specific languages
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you add support for function calls like max(a, b) or round(amount, 2)?
  • How would you optimize evaluation for rules that are checked millions of times?
  • How would you validate that a rule expression is safe before allowing it to be saved?
  • How would you add support for list operations like 'state IN [CA, NY, TX]'?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem at hand requires us to parse a domain-specific language (DSL) that describes financial rules using various operators and variable names. Given the nature of the expression evaluation invol...

Approach
  1. Tokenization: First, we need to transform the input expression string into a list of tokens. For example, the input 'credit_score >= 600 AND debt_to_income < 0.4 AND loan_amount <= max_approved...

Submit Your Answer
Markdown supported

Related Questions