Perfect AND Tree Leaf Updates
Updated
Problem statement
A perfect binary tree has binary values. Its leaves are given left to right in leaves, and every internal node is the bitwise AND of its two children: it is 1 exactly when both children are 1.
Process each row of operations in order. A row is ["SET", index] or ["CLEAR", index], using a zero-based leaf index. SET changes that leaf to 1; CLEAR changes it to 0. Repair every ancestor after the leaf change.
Return the root value after each operation.
Function
applyAndTreeUpdates(leaves: int[], operations: String[][]) → int[]Examples
Example 1
leaves = [1,1,1,1]operations = [["CLEAR","2"],["SET","2"],["CLEAR","0"]]return = [0,1,0]Clearing leaf 2 makes one ancestor and the root zero. Setting it restores an all-one tree; clearing leaf 0 makes the root zero again.
Example 2
leaves = [0,1]operations = [["SET","0"],["CLEAR","1"],["SET","1"]]return = [1,0,1]With two leaves, the root is their AND after every update.
Constraints
1 <= leaves.length <= 131072, andleaves.lengthis a power of two.- Every leaf value is either
0or1. 1 <= operations.length <= 200000.- Every operation is
SETorCLEARfollowed by a valid zero-based leaf index.
More Pure Storage interview questions
- One-Shot Callback Event DispatchONSITE INTERVIEW · Seen Sep 2026
- Rasterize a Circle with Integer PixelsONSITE INTERVIEW · Seen Sep 2026
- Set Command SimulationONSITE INTERVIEW · Seen Aug 2024
- Constant-Time Set OperationsPHONE SCREEN · Seen Sep 2026
- Racing ResultsOA · Seen Oct 2024
- Find DoublesOA · Seen Oct 2024
- Count Different Palindrome SubstringsOA · Seen Jul 2024
- Count Palindromic SubstringsOA · Seen Jul 2024
- Find RepetitionsOA · Seen Nov 2023
- Bakery Quality ControlOA · Seen Nov 2023
Similar questions at other companies
- Subtree Access OverridesPinterest · PHONE SCREENPinterest interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Nested Todo ListNotion · PHONE SCREEN · ONSITE INTERVIEWNotion interview questions
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Dictionary Records to an Editable XML TreeSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Inherited Role PermissionsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- In-Memory File System With Directory SizesCapital One · PHONE SCREEN · ONSITE INTERVIEWCapital One interview questions
- Colored Rooted TreeRubrik · ONSITE INTERVIEWRubrik interview questions