Boolean Parenthesization
A hard Dynamic Programming interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Dynamic Programming
- Sheets
- 1
- Core for
- 9 roles
The problem
Given a boolean expression consisting of symbols T and F and operators &, |, and ^, count the number of ways to parenthesize the expression so that it evaluates to true. Each symbol and operator must be used exactly once in order.
Example 1
- Input
- exp = "T&F|T^F"
- Output
- 5
- Why
- Splitting T&F|T^F at each operator, the DP counts 5 groupings that evaluate to true.
Example 2
- Input
- exp = "F|T&F"
- Output
- 0
- Why
- No parenthesization of F|T&F can evaluate to true since F|anything with &F at the end yields false when grouped as F|(T&F) or (F|T)&F.
Example 3
- Input
- exp = "F"
- Output
- 0
- Why
- A single F cannot evaluate to true.
Constraints
- 1 <= exp.length <= 200
- exp consists of 'T', 'F', '&', '|', '^' characters
- exp has an odd number of characters with symbols at even indices
How to think about it
Updated 2026-09-29Operators like XOR and AND depend on both the number of true ways and the number of false ways produced by sub-expressions. At every candidate split operator k between i and j, combine the true/false counts of the left sub-expression (i..k-1) with the right sub-expression (k+1..j) using boolean truth tables.
Approaches, worst first
Recursive expression partitioning
time O(4^n / n^(3/2)) · space O(n)
Recurse on interval (i, j, isTrue). Split at every operator between i and j, recursing for both true and false states. Evaluates Catalan-numbered parenthesizations with redundant intervals.
Interval DP with dual true/false tablesWrite this one
time O(n^3) · space O(n^2)
Maintain trueDP[i][j] and falseDP[i][j] storing the number of ways expression exp[i..j] evaluates to true or false. Iterate expression lengths, split at operators k, and apply truth tables for &, |, and ^.
Where people lose marks · 3
- Failing to track false evaluations: boolean operators like '^' and '|' require knowing the count of false sub-evaluations to compute the true count.
- Modulo arithmetic: intermediate multiplications (leftTrue * rightTrue) can overflow standard integers without modulo reduction.
- Stepping operator indices incorrectly: operators only occur at odd indices; stepping by 1 instead of 2 attempts to split on boolean literals.
The theory behind it
Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems
What Dynamic Programming is
Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.
When to reach for it
Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.
How the pattern works
Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.
More Dynamic Programming problems
Target Roles
Core requirement for 9 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .