Implement tree ops and LRU cache
Company: SoFi
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates proficiency with tree-based mutable data structures and cache design, specifically maintaining aggregated state in a full binary tree after leaf updates and implementing an LRU cache supporting O(1) get/put operations.
Constraints
- 1 <= n <= 200000
- n is a power of two
- 1 <= len(ops) <= 200000
- Each operation is a pair (cmd, i) where cmd is 'set' or 'clear' and 0 <= i < n
- Updates should be O(log n) per operation; overall O(len(ops) log n)
Hints
- Model the updates with a segment tree using bitwise AND as the merge function.
- Store values in an array of size 2*n with leaves at indices [n .. 2*n-1].
- On updating a leaf, recompute ancestors up to the root, and you can stop early if a node's value does not change.