PPractick

DSA interview question

Different Ways to Evaluate a Boolean Expression

Medium · Algorithms · Topics: dynamic programming, mcm, recursion

The problem

You're building a rule-engine for a fraud-detection system at a fintech company. Analysts write boolean rules like "1&0|1" (using 1/0 for true/false and &, |, ^ for AND/OR/XOR) but forget to specify operator precedence or parentheses in legacy rule files migrated from an old system. Since ambiguous expressions could evaluate differently depending on how they're grouped, your task is to compute every possible result obtainable by parenthesizing the expression in all valid ways — so the compliance team can audit whether the rule is ambiguous (multiple distinct outcomes) or safe (all groupings agree).

Given a string expression containing digits 0/1 and operators &, |, ^, return a list of all possible results from evaluating the expression by computing every possible way to parenthesize the operators.

Function signature

vector<int> diffWaysToEvaluate(string expression);

Test cases

Input expressionOutputExplanation
`"1&01"`[0, 1]
`"0&0&0&1^10"`[0, 1]
"1"[1]Single operand, no operators — trivial base case
"0"[0]Single operand
`"1^001"`

(Constraints typically: 1 <= expression.length <= 20, expression consists of digits 0/1 and operators &,|,^ only, no spaces, no unary operators)

(Note: since these are boolean 0/1 results, the output is just every distinct {0,1} value achievable — but the classic LeetCode version of this problem uses arithmetic +,-,* and returns ALL results including duplicates, not deduplicated. The boolean variant with &,|,^ is a common interview spin — clarify with interviewer whether to dedupe.)

How interviewers phrase it

Clarifying questions to ask

  1. Should the output contain duplicate values, or should it be deduplicated to a set? (Arithmetic LC241 keeps duplicates since results are usually distinct numbers; the boolean variant naturally has many duplicates since there are only 2 possible values — confirm whether to dedupe.)
  2. What operators are supported — just &, |, ^? Any operator precedence rules to ignore, or do we treat every split point equally regardless of operator type?
  3. Can the expression contain multi-digit operands, or is it guaranteed single-digit (0/1) only? (Changes how you parse operands during recursion/splitting.)
  4. What's the max expression length? (For length ~20, the number of parenthesizations follows Catalan number growth — without memoization this can blow up; confirm if memoization is expected.)

Follow-ups to expect

  1. "Can you memoize to avoid recomputation?"
  2. "What's the time complexity, and why does it grow so fast?"
  3. *"Can you do this with arithmetic operators (+, -, ) instead?"
  4. "What if you only wanted to know whether the expression is ambiguous (more than one distinct result), not list every result?"
  5. "How would you extend this to support multi-digit numbers in the boolean version (e.g., bitwise AND on integers, not just 0/1)?"
  6. "Can you do this iteratively with DP instead of top-down recursion?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in