Parse and Evaluate Expense Policy Rules Written as Expressions
Last updated: November 29, 2026
Quick Overview
Finance admins write spend rules like `amount > 500 AND (category == "travel" OR merchant == "Uber") AND NOT day == "Sun"`. Write a tokenizer, a parser with correct precedence, and an evaluator that checks a transaction against a rule.
Ramp
November 29, 20260
0
0 solved
Finance admins write spend rules like `amount > 500 AND (category == "travel" OR merchant == "Uber") AND NOT day == "Sun"`. Write a tokenizer, a parser with correct precedence, and an evaluator that checks a transaction against a rule.
Ramp's spend controls let customers express which purchases need review or are blocked, and an expression language is a natural way to power that. The interviewer is checking parsing fundamentals: tokenizing with literals and escapes, recursive descent with NOT over AND over OR precedence, clear syntax errors, and a parse-once, evaluate-many design.
What the Interviewer Expects
- Separates tokenizing, parsing to a tree, and evaluating
- Writes a grammar with precedence NOT > AND > OR and parentheses
- Handles quoted strings with escapes and numeric literals
- Reports syntax errors for dangling operators, unbalanced parens and trailing input
- Parses once and evaluates the tree against many transactions
- Decides how missing fields and type mismatches behave
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you add `IN ("a", "b")` and `CONTAINS` operators?
- How would you show the admin which clause caused a transaction to be flagged?
- How would you safely let rules reference nested fields like `merchant.mcc`?
- Why not just use Python's eval?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Support comparisons == != > >= < <= between a field and a number or quoted string, combined with AND, OR, NOT and parentheses. AND binds tighter than OR, NOT tighter than AND. Invalid rules fa...
High-Level Design
A regex tokenizer emits NUM, STR, OP, IDENT and keyword tokens and rejects any character it cannot consume. The grammar is
or := and (OR and)*, and := not (AND not)*, `not := NOT not | at...