Parsing A Boolean Expression

HardStackstack

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Evaluate a boolean expression string built from these forms: - 't' is true and 'f' is false. - '!(e)' is the logical NOT of the single inner expression e. - '&(e1,e2,...,en)' is the logical AND of one or more comma-separated inner expressions. - '|(e1,e2,...,en)' is the logical OR of one or more comma-separated inner expressions. Return the value of the whole expression as a boolean. The input is always well-formed and contains no spaces.

Examples

Input: expression = "|(f,&(t,t),!(t))"
Output: true
`&(t,t)` is true, so the OR is true even though `f` and `!(t)` are false.
Input: expression = "&(t,!(|(t,f)))"
Output: false
`|(t,f)` is true, its negation is false, so the AND is false.

Constraints

  • `1 <= expression.length <= 2 * 10^4`
  • `expression[i]` is one of `(`, `)`, `&`, `|`, `!`, `t`, `f`, `,`
  • `expression` is a valid expression as described

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More stack problems