KatInterview

Mini Policy Engine: Boolean Rule Evaluation for Post Routing

StringsAuto-graded · 22 test cases
HardAuto-gradedStringsparsingrecursionstring processing

Last updated

Tests whether a candidate can build a recursive-descent parser for a small boolean expression language and wire it into a first-match rule dispatcher.

Mini Policy Engine: Boolean Rule Evaluation for Post Routing

You are building the routing layer for a community platform. Every submitted post must be placed into exactly one of three queues — SHOW, HOLD, or TAKEDOWN — by evaluating a list of human-readable rules against the post's fields.

Posts and rules

A post is a plain dictionary mapping field names (strings) to values that are either numbers or strings, for example:

{"author_strikes": 2, "region": "EU", "word_count": 40, "hours_since_signup": 3}

This is only illustrative; do not assume a fixed schema.

A rule is a pair (condition_string, action) where action is one of "SHOW", "HOLD", or "TAKEDOWN".

Given an ordered list of rules and a batch of posts, return one action per post in the same order.

Dispatching

Evaluate the rules for each post from first to last and return the action of the first rule whose condition is true. If no rule matches, return "SHOW" for that post.

Condition language

Condition strings are built from three layers, each adding to the last.


Layer 1 — single comparison

field OP literal
  • field is an identifier ([A-Za-z_][A-Za-z0-9_]*).
  • OP is one of ==, !=, >, >=, <, <=.
  • literal is either a number (integer or decimal, optionally negative) or a double-quoted string such as "EU".

A comparison is strictly false — never an error — when:

  • the field is absent from the post, or
  • the types of the post value and the literal are incompatible (e.g., applying > between a string field and a numeric literal).

Layer 2 — chained AND / OR, no operator precedence

Multiple comparisons may be joined with AND or OR. There is no precedence between them: the expression is evaluated strictly left to right. For example:

region == "EU" OR region == "UK" AND hours_since_signup < 1

groups as:

(region == "EU" OR region == "UK") AND hours_since_signup < 1

Layer 3 — parentheses

Parentheses override left-to-right grouping and may be nested to any depth:

(region == "EU" OR region == "UK") AND author_strikes == 0

An unbalanced parenthesis must raise a ValueError.


Constraints

  • 1 <= len(rules) <= 50
  • 1 <= len(posts) <= 500
  • Each condition string is between 1 and 500 characters long.
  • Every action in the input is one of "SHOW", "HOLD", "TAKEDOWN"; raise ValueError for anything else.
  • Field names in conditions may refer to fields the post does not have; this makes only that comparison false.
  • Numeric literals are integers or decimals (e.g., 3, -1, 2.5); scientific notation is not required.
  • String literals are enclosed in double quotes and contain no escaped characters beyond \" and \\.
  • AND and OR are case-insensitive keywords; field names that spell out and or or are not valid input.

Examples

Example 1

Input: moderate_posts([['author_strikes > 3', 'TAKEDOWN'], ['(region == "EU" OR region == "UK") AND hours_since_signup < 1', 'HOLD']], [{'id': 'q7', 'author_strikes': 1, 'region': 'UK', 'hours_since_signup': 0}])

Output: ['HOLD']

Example 2

Input: moderate_posts([['word_count > 50', 'HOLD']], [{'word_count': 75}, {'word_count': 50}])

Output: ['HOLD', 'SHOW']

Hints

Hint 1

Break the work into four independent pieces: tokenizing the text into a flat token list, parsing a single comparison from three consecutive tokens, recursively parsing a full boolean expression (with parentheses delegating back to the top-level parse function), and evaluating a parsed tree against one post. Keeping these separate means later changes only ever touch one piece.

Hint 2

For left-to-right AND/OR with no precedence, use a running-accumulator pattern rather than a precedence-climbing parser. Parse the first term into a node, then loop: if the next token is AND or OR, consume it, parse the next term, and immediately combine the new term with *everything accumulated so far* into a new node. By the time AND is seen in `A OR B AND C`, the node for `A OR B` is already th…

Hint 3

To handle parentheses without any extra parsing logic, extend the 'parse one term' step so that when it sees a `(`, it calls the top-level expression parser recursively and then expects a matching `)`. Nesting falls out automatically from the recursion — no depth tracking needed.