FastPrepParse Boolean Rule Expressions
Problems/Parse Boolean Rule Expressions

Parse Boolean Rule Expressions

Stripe logoStripe● HardFULLTIMEINTERNNEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Stripe phone screen. The reports from the original authors describe a rule-parser exercise but do not keep its original grammar or the result format. We cover about 20% of the details that the sources back. We chose the Boolean grammar, the operators and precedence, one fixed way to write the syntax tree, the INVALID policy, the examples and the input limits. The original README that was supplied is not available.

You are given one string rule that holds a Boolean rule expression. Parse it into a syntax tree and return that tree written in prefix form, using the fixed format below. If the whole input is not a valid expression, return INVALID instead.

Tokens

  • An identifier starts with an ASCII letter or underscore. After that it may use ASCII letters, digits, and underscores. The uppercase words NOT, AND, and OR are reserved keywords.
  • An integer literal is one or more decimal digits, with an optional + or - sign in front.
  • A string literal sits inside double quotes and holds printable ASCII characters. Inside a string, \" stands for a quote and \\ stands for a backslash. No other escape is valid.
  • The comparison operators are ==, !=, <, <=, >, and >=.
  • Outside strings, spaces, tabs, carriage returns, and newlines are ignored. Keywords are case-sensitive.

Grammar and precedence

Every comparison has exactly the form identifier comparison-operator literal. The literal is an integer or a string. The Boolean operators work on comparisons and on expressions in parentheses.

  1. NOT has the highest precedence of the Boolean operators and groups from the right. So you may write NOT several times in a row.
  2. AND comes next and groups from the left.
  3. OR has the lowest precedence and groups from the left.
  4. Parentheses can change this order, and they can be nested.

Canonical serialization

  • A comparison is written as (operator identifier literal).
  • A negation is written as (NOT expression).
  • An AND is written as (AND left right), and an OR as (OR left right).
  • Integer literals drop a leading + and any leading zeros. Every zero, including negative zero, is written as 0.
  • String literals stay in quotes. In the result they use only the \" and \\ escapes.

Return INVALID for the whole input if it has a token that cannot be read (a lexical error), an incomplete comparison, an unexpected token, an empty expression, or mismatched parentheses.

Function

parseRuleExpression(rule: String) → String

Examples

Example 1

rule = "age >= 18 AND country == \"US\""return = "(AND (>= age 18) (== country \"US\"))"

The rule has two comparisons, one on age and one on country. Each one is written in prefix form first. AND joins the two, so it becomes the root, which gives the output shown.

Example 2

rule = "NOT (status == \"blocked\" OR retries > +003)"return = "(NOT (OR (== status \"blocked\") (> retries 3)))"

The parentheses group the two comparisons under OR, and NOT applies to that whole group. So OR is the only child of NOT. The literal +003 loses its plus sign and leading zeros and is written as 3.

Example 3

rule = "age >= AND active == 1"return = "INVALID"

The first comparison has no literal after >=. That is an incomplete comparison, so the answer is INVALID.

Constraints

  • The input is an ASCII string and contains at most 200000 tokens.
  • Parenthesis nesting is at most 1000.
  • Every identifier and every decoded string literal has length at most 100.
  • Inputs that violate the token, nesting, or token-length rules return INVALID.

More Stripe Phone Screen questions

  • Business Account KYC Verification — Parts 1–5PHONE SCREEN · Seen Nov 2026
  • Financial Account LedgerOA · PHONE SCREEN · Seen Nov 2026
  • Merchant Matching, Parts 1–2: Shared Fields and Weighted ThresholdsPHONE SCREEN · 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
  • Render a Task Tree from CSV RowsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Invoice / Payment ReconciliationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Linked Merchants Within Two HopsPHONE SCREEN · Seen Nov 2026
  • Portfolio Valuation, Part 1PHONE SCREEN · Seen Nov 2026
  • Account Roles With Parent-User InheritancePHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • Evaluate a Parenthesized Integer ExpressionAdobe · PHONE SCREENAdobe interview questions
  • Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions
  • Number of AtomsAmazon · ONSITE INTERVIEW
  • Evaluate an Arithmetic Expression with DivisionMotive · ONSITE INTERVIEWMotive interview questions
  • Arithmetic Expression CalculatorSnowflake · PHONE SCREENSnowflake interview questions
  • Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
  • Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
  • Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
  • Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions

Interview reports that mention this question

  • Stripe Experienced SWE OA and Onsite Questions: 3 Experiences
  • Stripe AI Coding Interview, Experienced SWE: 6 Experiences
  • Stripe New Grad SWE Onsite and HM Round: 1 Experience
  • Stripe SWE Internship Reconciliation, AI Coding, and Bikemap Loop

Stripe interview experiences

  • Stripe New Grad SWE OA and Phone Screen: 2 Experiences
  • Stripe SWE Intern Linked Merchants Team Screen: 1 Experience
All Stripe interview questionsSee Stripe hiring insights
Saved
class Solution {
    public String parseRuleExpression(String rule) {
      // write your code here
    }
}
Checking account…