Represent and Evaluate Boolean Predicate Expressions
Design a data structure to represent and evaluate boolean predicate expressions. The expressions can include comparisons (<, >, <=, >=, ==), logical operators (and, or), and parentheses. The system should support evaluation against a dictionary of variable values, where variables map to numeric values. Input expressions can be parsed before evaluation, and short-circuit evaluation is desirable.
Constraints & Assumptions
- Variables map to numeric values.
- Operators include
<,>,<=,>=,==,and, andor. - Input can be parsed before evaluation.
- Short-circuit evaluation is desirable.
Follow-up Questions
- How would you add function calls?
- How would you compile predicates for faster repeated evaluation?
- How would you serialize the Abstract Syntax Tree (AST)?
- How would you prevent unsafe arbitrary code execution?
AI interview prep
Core dilemma
Balancing the expressiveness and complexity of a flexible predicate language against the efficiency and safety of its evaluation and parsing.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Represent and Evaluate Boolean Predicate Expressions
Design a data structure to represent and evaluate boolean predicate expressions. The expressions can include comparisons (<, >, <=, >=, ==), logical operators (and, or), and parentheses. The system should support evaluation against a dictionary of variable values, where variables map to numeric values. Input expressions can be parsed before evaluation, and short-circuit evaluation is desirable.
Constraints & Assumptions
- Variables map to numeric values.
- Operators include
<,>,<=,>=,==,and, andor. - Input can be parsed before evaluation.
- Short-circuit evaluation is desirable.
Follow-up Questions
- How would you add function calls?
- How would you compile predicates for faster repeated evaluation?
- How would you serialize the Abstract Syntax Tree (AST)?
- How would you prevent unsafe arbitrary code execution?