KatInterview

Build a Rule-Based Access Control Evaluator with Expression Parsing

Data Structure DesignModel answer
MediumModel answerData Structure Designparsinginterpretersrecursion

Last updated

Design and implement an access decision engine that evaluates ordered security rules with conditions expressed in a custom expression language.

You're building an access control system for authentication requests. The system receives login attempts as dictionaries of attributes and must evaluate them against a sequence of security rules.

def decide(attempt: dict, policies: list[tuple[str, str]]) -> str:
    ...

Inputs:

  • attempt: dictionary mapping attribute names to values (strings or booleans)
  • policies: ordered list of (verdict, condition) tuples where verdict is "allow" or "deny"

Behavior:

  • Evaluate rules in order; the first matching condition determines the outcome
  • If no rule matches, default to "allow"
  • All conditions are guaranteed to be syntactically correct

Expression Language Rules:

Literals and Identifiers:

  • String constants use single quotes: 'US'
  • Escape sequences: \' for quote, \\ for backslash
  • Identifiers match [A-Za-z_][A-Za-z0-9_]* and reference attempt attributes
  • Attributes absent from attempt cause any expression using them to evaluate false

Phase 1: Equality Comparisons

Support operand = operand where operands are attributes or string literals in any order.

attempt = {"country": "US", "client": "mobile"}
policies = [
    ("deny", "country = 'CN'"),
    ("allow", "'mobile' = client"),
    ("deny", "client = 'mobile'")
]
decide(attempt, policies)  # "allow" - second rule matches first

Phase 2: Boolean Logic

Attributes can be boolean. A standalone identifier evaluates true when its value is exactly True. Add and / or operators (lowercase keywords), where and has higher precedence.

attempt = {"country": "US", "suspicious": True, "verified": False}
decide(attempt, [
    ("deny", "suspicious and verified or country = 'IR'"),
    ("deny", "country = 'US' and suspicious")
])  # "deny" - second rule matches

The first condition parses as (suspicious and verified) or country = 'IR' and evaluates false.

Phase 3: Complete Expression Support

Add parentheses for grouping, prefix not operator (higher precedence than and), and != for inequality. Inequality also returns false when either operand is missing.

attempt = {"country": "FR", "suspicious": True, "verified": False, "client": "web"}
decide(attempt, [
    ("deny", "suspicious and not (verified or client = 'kiosk')"),
    ("allow", "missing_field"),
])  # "deny"

decide({"country": "FR", "client": "it\'s"}, [
    ("deny", r"client = 'it\'s' and country != 'DE'")
])  # "deny"

Performance Consideration: Expect thousands of rules checked against many requests. Consider what preprocessing can be done once per rule.

Hints

Hint 1

Separate tokenization from parsing from evaluation - only one of these depends on the request data

Hint 2

Handle missing attributes with a sentinel value that makes comparisons return false consistently

Hint 3

Use recursive descent parsing with one function per precedence level to handle operator priority naturally