Evaluate a nested boolean expression with NOT, AND and OR operators

Quick Overview

Evaluate a boolean expression string built from t, f and nested NOT, AND and OR groups such as &(|(f)) and return its truth value. It tests parsing nested parentheses in one pass with a stack and applying each operator to its operands.

Evaluate a nested boolean expression with NOT, AND and OR operators

Company: Weights & Biases

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

A boolean expression is an expression that evaluates to either `true` or `false`. It can have one of the following shapes: - `'t'`, which evaluates to `true`. - `'f'`, which evaluates to `false`. - `'!(subExpr)'`, which evaluates to the logical NOT of the inner expression `subExpr`. - `'&(subExpr1,subExpr2,...,subExprn)'`, which evaluates to the logical AND of the inner expressions `subExpr1, subExpr2, ..., subExprn`, where `n >= 1`. - `'|(subExpr1,subExpr2,...,subExprn)'`, which evaluates to the logical OR of the inner expressions `subExpr1, subExpr2, ..., subExprn`, where `n >= 1`. Given a string `expression` that represents a boolean expression, return the evaluation of that expression. It is guaranteed that the given expression is valid and follows the given rules. ### Function Signature ```python def parse_bool_expr(expression: str) -> bool: ``` ### Rules - Return `True` when the expression evaluates to true and `False` otherwise. - The string contains no spaces. The subexpressions inside `&(...)` and `|(...)` are separated by commas only. - `!(...)` always holds exactly one subexpression. ### Constraints - `1 <= len(expression) <= 2 * 10^4` - `expression[i]` is one of `'('`, `')'`, `'&'`, `'|'`, `'!'`, `'t'`, `'f'` and `','`. ### Examples ```text Input: expression = "&(|(f))" Output: False ``` First `|(f)` evaluates to false. Then `&(false)` is false, so the result is `False`. ```text Input: expression = "|(f,f,f,t)" Output: True ``` The OR of false, false, false and true is true because at least one operand is true. ```text Input: expression = "|(&(t,f,t),!(f))" Output: True ``` `&(t,f,t)` is false because one operand is false, and `!(f)` is true. The OR of false and true is true.

Overview: Evaluate a boolean expression string built from t, f and nested NOT, AND and OR groups such as &(|(f)) and return its truth value. It tests parsing nested parentheses in one pass with a stack and applying each operator to its operands.

Weights & Biases logo
Weights & Biases
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

A boolean expression is an expression that evaluates to either true or false. It can have one of the following shapes:

  • 't' , which evaluates to true .
  • 'f' , which evaluates to false .
  • '!(subExpr)' , which evaluates to the logical NOT of the inner expression subExpr .
  • '&(subExpr1,subExpr2,...,subExprn)' , which evaluates to the logical AND of the inner expressions subExpr1, subExpr2, ..., subExprn , where n >= 1 .
  • '|(subExpr1,subExpr2,...,subExprn)' , which evaluates to the logical OR of the inner expressions subExpr1, subExpr2, ..., subExprn , where n >= 1 .

Given a string expression that represents a boolean expression, return the evaluation of that expression.

It is guaranteed that the given expression is valid and follows the given rules.

Function Signature

def parse_bool_expr(expression: str) -> bool:

Rules

  • Return True when the expression evaluates to true and False otherwise.
  • The string contains no spaces. The subexpressions inside &(...) and |(...) are separated by commas only.
  • !(...) always holds exactly one subexpression.

Constraints

  • 1 <= len(expression) <= 2 * 10^4
  • expression[i] is one of '(' , ')' , '&' , '|' , '!' , 't' , 'f' and ',' .

Examples

Input: expression = "&(|(f))"
Output: False

First |(f) evaluates to false. Then &(false) is false, so the result is False.

Input: expression = "|(f,f,f,t)"
Output: True

The OR of false, false, false and true is true because at least one operand is true.

Input: expression = "|(&(t,f,t),!(f))"
Output: True

&(t,f,t) is false because one operand is false, and !(f) is true. The OR of false and true is true.