Dsa
Recursion
Medium
The Alternating Gate Logic
Evaluate a boolean expression tree. Nodes are operations: 'A' (AND), 'O' (OR), 'X' (XOR). A node is formatted as 'Op(arg1,arg2,...)'. A leaf is just '0' or '1'.
Crucially, at every ODD depth (root is depth 0), the gate's logic is inverted (AND becomes NAND, OR becomes NOR, XOR becomes XNOR).
Example: 'A(1,0)' at Depth 0.
- Root (Depth 0) is 'A' (AND). 1 AND 0 = 0.
Example: 'A(1,0)' at Depth 1.
- Root is Depth 1 (Odd), so 'A' is NAND. 1 NAND 0 = 1.
Input: A string expression.
Output: The final bit (0 or 1).
Key concepts
recursionboolean logictrees
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now