Perfect Binary Tree Bit Propagation
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a perfect binary tree whose leaves hold bits (`0` or `1`). The constructor receives a nonempty array `bits` with a power-of-two length `N`, giving the leaves from left to right. Every internal node stores the bitwise AND of its two children.
Leaves can be set to `1` or cleared to `0` one at a time. Each update must propagate to the leaf's ancestors in `O(log N)` time, and reading the value of any node must take `O(1)` time.
Leaf indexes are zero-based positions within `bits`. Node indexes use zero-based heap order: the root is node `0`, the children of node `i` are `2i + 1` and `2i + 2`, and the leaves are nodes `N - 1` through `2N - 2`. All inputs are valid.
Implement a class `BitwiseTree` with the following methods:
- `BitwiseTree(bits: List[int])`: build the tree from the leaf bits.
- `setBit(leafIndex: int)`: set leaf `leafIndex` to 1 and recompute its ancestors in `O(log N)`.
- `clearBit(leafIndex: int)`: set leaf `leafIndex` to 0 and recompute its ancestors in `O(log N)`.
- `nodeValue(nodeIndex: int) -> int`: return the stored value (0 or 1) of the node at heap index `nodeIndex` in `O(1)`.
Example 1:
```text
Input:
["BitwiseTree", "nodeValue", "nodeValue", "nodeValue", "setBit", "nodeValue", "nodeValue", "nodeValue"]
[[[1, 0, 1, 1]], [0], [1], [2], [1], [1], [0], [4]]
Output: [null, 0, 0, 1, null, 1, 1, 1]
Explanation:
tree = BitwiseTree([1, 0, 1, 1]) # leaves are nodes 3..6: [1, 0, 1, 1]
tree.nodeValue(0) # -> 0 (root = node1 AND node2 = 0 AND 1)
tree.nodeValue(1) # -> 0 (1 AND 0)
tree.nodeValue(2) # -> 1 (1 AND 1)
tree.setBit(1) # leaf 1 (node 4) = 1, node 1 = 1, root = 1
tree.nodeValue(1) # -> 1
tree.nodeValue(0) # -> 1 (all leaves are now 1)
tree.nodeValue(4) # -> 1 (leaf 1)
```
Example 2:
```text
Input:
["BitwiseTree", "nodeValue", "setBit", "nodeValue", "setBit", "nodeValue", "clearBit", "nodeValue"]
[[[0]], [0], [0], [0], [0], [0], [0], [0]]
Output: [null, 0, null, 1, null, 1, null, 0]
Explanation:
tree = BitwiseTree([0]) # N = 1, root is also the only leaf
tree.nodeValue(0) # -> 0
tree.setBit(0) # leaf 0 = 1
tree.nodeValue(0) # -> 1
tree.setBit(0) # repeated set, no change
tree.nodeValue(0) # -> 1
tree.clearBit(0) # leaf 0 = 0
tree.nodeValue(0) # -> 0
```
```hint Where each node lives
The heap-order numbering maps every node to one slot of a flat array of size `2N - 1`. Work out the array index of leaf `i` and of a node's parent from the child formulas.
```
```hint Shortening the walk up
After you recompute a parent, compare its new value with the old one. Ask what that comparison tells you about the ancestors above it.
```
### Constraints & Assumptions
- `1 <= bits.length <= 2¹⁶`
- `bits.length` is a power of two
- `bits[i]` is `0` or `1`
- `0 <= leafIndex < bits.length`
- `0 <= nodeIndex <= 2 * bits.length - 2`
- At most `10⁴` calls will be made to `setBit`, `clearBit`, and `nodeValue`.
### What a Strong Answer Covers
- A storage layout that reaches any node by its heap index in `O(1)`, including the leaf offset and the parent formula
- A bottom-up construction in `O(N)` that computes children before their parents
- An update that recomputes each affected ancestor from both of its children, in `O(log N)`
- When the upward walk can stop early, and why stopping there is safe
- The `N = 1` case, where the root is also the only leaf, and updates that write a value the leaf already holds
### Follow-up Questions
- If internal nodes stored the XOR of their children, or the count of set leaves below them, what changes in the update, and does stopping early still help?
- How would you find the index of the leftmost leaf that holds `0` in `O(log N)`?
- How would you support setting every leaf in a range `[l, r]` to `1` faster than one `setBit` call per leaf?
Overview: Implement a perfect binary tree over an array of leaf bits in which every internal node holds the bitwise AND of its two children. It tests array-based tree layout, propagating single-leaf updates to the root in logarithmic time, and constant-time reads of any node.