Evaluate Ordered Action Rules
Updated
Problem statement
You are given ordered decision rules and one transaction represented by named attributes. Each rule has the form ACTION if CONDITION, where ACTION is ACCEPT or BLOCK.
Visit rules in their given order. Return the action of the first rule whose complete condition is true. Stop at that rule: an earlier matching ACCEPT wins over a later matching BLOCK, and an earlier matching BLOCK also wins over a later ACCEPT. If no rule matches, return ACCEPT.
Attributes and comparisons
Each attribute is a string of the form field=value. A value is a signed decimal integer, a lowercase Boolean true or false, or a double-quoted string. Field names begin with an ASCII letter or underscore and continue with ASCII letters, digits, or underscores. The words AND, OR, true, and false are reserved.
An atomic comparison contains one field name, one comparison operator, and one literal of the field's type. The field and literal may appear on either side, as in merchant = "Sand and Stone" or "Sand and Stone" == merchant. The operators = and == both mean equality; != means inequality. Integers also support <, <=, >, and >=. Compare integers by numeric value, including when a value has a leading sign or zeros. Compare strings exactly and case-sensitively.
Compound conditions
Combine comparisons with uppercase AND and OR. For this exercise, AND has higher precedence than OR; each operator associates to the left. Parentheses may be nested and override precedence. Spaces, tabs, carriage returns, and newlines may separate tokens, including the action, lowercase if, and condition. Such whitespace outside quoted strings is ignored. Strings contain printable ASCII characters; inside a string, \" represents a quote and \\ represents a backslash. Text such as and, or, AND, OR, parentheses, or comparison symbols inside quotes remains part of that string.
All input follows this grammar. Every referenced field exists, field names are unique in attributes, and each comparison uses two values of the same type. You do not need to handle invalid input.
Progressive practice
- Start with string equality and ordered
ACCEPT/BLOCKrules, including the defaultACCEPT. - Add Boolean comparisons and
AND/OR. Keep all earlier tests, especially quoted merchant names that contain connector words. - Add parentheses and mixed conditions. Separate tokenization, parsing, comparison, and rule selection so each part can be tested. An abstract syntax tree with recursive evaluation is one valid design; a parser that evaluates as it reads is also valid.
Function
evaluateOrderedRules(rules: String[], attributes: String[]) → StringExamples
Example 1
rules = ["BLOCK if fraud_flag=true","ACCEPT if trusted_partner=true","BLOCK if amount>10000"]attributes = ["fraud_flag=false","trusted_partner=true","amount=15000"]return = "ACCEPT"The first rule does not match. The trusted-partner rule matches and returns ACCEPT, so the later amount rule is not evaluated.
Example 2
rules = ["BLOCK if amount>100","ACCEPT if trusted_partner=true"]attributes = ["amount=200","trusted_partner=true"]return = "BLOCK"The first rule already matches, so insertion order makes its BLOCK result final.
Example 3
rules = ["ACCEPT if trusted_partner=true AND amount<=5000","BLOCK if fraud_flag=true OR amount>=20000"]attributes = ["trusted_partner=true","amount=8000","fraud_flag=false"]return = "ACCEPT"The conjunction fails because the amount is too large, and both parts of the disjunction are false. No rule matches, so the default result is ACCEPT.
Example 4
rules = ["ACCEPT if \"Sand and Stone\" == merchant","BLOCK if flagged=true"]attributes = ["merchant=\"Sand and Stone\"","flagged=true"]return = "ACCEPT"The first rule matches the complete merchant name. The word and inside quotes is string data. The result is ACCEPT, so the later matching block rule cannot change it.
Example 5
rules = ["BLOCK if (country=\"US\" OR country=\"CA\") AND flagged=true","ACCEPT if trusted=true"]attributes = ["country=\"CA\"","flagged=false","trusted=true"]return = "ACCEPT"The country group is true, but flagged=true is false, so the first rule does not match. The second rule matches and returns ACCEPT.
Example 6
rules = ["ACCEPT\tif\ttrusted=true","BLOCK\r\nif\r\namount>=50"]attributes = ["trusted=false","amount=50"]return = "BLOCK"The first condition is false, and the second condition is true at the amount boundary. Tabs and line breaks separate tokens just as spaces do, so the second rule returns BLOCK.
Constraints
0 <= rules.length, attributes.length <= 10000.- Each rule contains between
1and10000ASCII characters. Each attribute contains between3and1000ASCII characters. - Each rule has at most
20atomic comparisons and parenthesis nesting at most100. - Every field name has at most
100characters. Every decoded string value has at most100characters and may be empty. - Every integer fits in a signed
64-bit value. Integer values are supplied as text inside the string arrays. - Every rule and attribute follows the stated grammar, every referenced field occurs exactly once in
attributes, and compared values have the same type.
More Stripe Phone Screen questions
- Split Tender Part 4 - RefundsOA · PHONE SCREEN · Seen Nov 2026
- Merchant Payout Reserve CalculatorPHONE SCREEN · Seen Nov 2026
- Parse Boolean Rule ExpressionsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Linked Merchants Within Two HopsPHONE SCREEN · Seen Nov 2026
- Merchant Matching, Parts 1–2: Shared Fields and Weighted ThresholdsPHONE SCREEN · Seen Nov 2026
- Business Account KYC Verification — Parts 1–5PHONE SCREEN · Seen Nov 2026
- Financial Account LedgerOA · PHONE SCREEN · Seen Nov 2026
- Invoice / Payment ReconciliationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merchant Payout Scheduler, Part 2: Per-Merchant SchedulesPHONE SCREEN · Seen Nov 2026
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesPHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Out-of-Order TLV File ReassemblyApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Parse Command-Line TokensSamsara · ONSITE INTERVIEWSamsara interview questions
- Reversible Run-Length EncodingZillow · PHONE SCREENZillow interview questions
- Exclusive Time of FunctionsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Layered ASCII Canvas Command ProcessorAsana · ONSITE INTERVIEW
- Format CSV Rows as an ASCII TableAmazon · PHONE SCREEN
- Bank Requests with Delayed CashbackRamp · OARamp interview questions
- Chunked Byte Stream ChecksumsUpstart · OAUpstart interview questions