Minimum Leaf Flips to Make a Feature Flag Expression Evaluate True

Last updated: November 29, 2026

Quick Overview

A boolean expression over feature flags is a binary tree: leaves are True or False, internal nodes are AND, OR, XOR, or NOT (with one child). Return the minimum number of leaf flips so the root evaluates to a given target.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A boolean expression over feature flags is a binary tree: leaves are True or False, internal nodes are AND, OR, XOR, or NOT (with one child). Return the minimum number of leaf flips so the root evaluates to a given target.

A Google onsite hard question about rollout logic, where a launch condition is a boolean tree over flags. The interviewer expects tree DP that returns the cost to make each subtree true and false, and careful per-operator combination rules.

What the Interviewer Expects
  • Return a pair (cost to be False, cost to be True) per subtree
  • Derive combination rules for AND, OR, XOR and NOT
  • Handle NOT nodes with a single child
  • Compute in one post-order pass
  • Explain why only leaf flips are allowed and how that shapes the base case
Key Topics to Cover
binary trees
tree dynamic programming
boolean expressions
post-order traversal
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Allow flipping operators too, with their own cost.
  • Return which leaves to flip.
  • What if the same flag appears in several leaves and flips must be consistent?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: expression tree root and boolean target. Leaves have values 0 or 1. Operators are 'AND', 'OR', 'XOR' with two children and 'NOT' with exactly one (left or right). Output: minimum number of le...

High-Level Design

Post-order DP returning (f, t) for each subtree: minimum flips to make it evaluate False and True. A leaf with value v costs 0 to keep v and 1 to flip. For NOT with child (f, t), return (t, f). ...


Submit Your Answer
Markdown supported