Data structure representing a two-value array with 3 operations
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Suppose you have a fixed-length boolean array that starts entirely false, supports one-way updates from false to true, and needs three operations: val(i) in O(1), change(i) in amortized O(1), and find(i) for the nearest false index in O(log n). That mix of requirements is unusual enough that a plain array or a plain balanced tree is not quite the right answer on its own.
Why the One-Way Update Matters
The crucial detail is that values only change in one direction:
- initial state: all
false - updates:
false -> true - never:
true -> false
That monotonic behavior makes a segment-tree-style summary structure attractive. Each internal node can store whether all values in its interval are true. If an interval is not fully true, then it still contains at least one false, which is exactly what find(i) needs to discover quickly.
Store the Raw Values Plus an Interval Tree
Keep two representations:
- a plain boolean array for
val(i)inO(1) - a segment tree where each node stores the logical
ANDof its interval
If a segment tree node is:
- '
true, then every element in that interval istrue' - '
false, then at least one element in that interval is stillfalse'
Here is a simple Python implementation:
val(i) is obviously O(1) because it is just an array lookup.
Why change(i) Can Be Amortized O(1)
In the worst individual case, updating a leaf can touch O(log n) nodes on the path to the root. But because values only move from false to true, each tree node flips from false to true at most once during the entire lifetime of the data structure.
That means total propagation work across many updates is bounded by the number of tree nodes, which gives amortized constant update cost over a long sequence of change operations.
This amortized argument would break immediately if values could change back to false.
Implement find(i) with Left and Right Searches
To find the closest false to index i, search left and right for the nearest interval that still contains a false, then take the closer result.
Each search descends the tree in O(log n) time because fully true intervals are skipped immediately.
Common Pitfalls
- Ignoring the one-way
false -> trueconstraint. That monotonicity is what makes the amortized bound plausible. - Trying to solve everything with only an array.
find(i)becomes too slow. - Using only a balanced tree of
trueorfalseindices and forgetting thatval(i)is required to be constant-time. - Assuming worst-case
change(i)isO(1). The guarantee here is amortized, not per-operation worst case.
Summary
- Use a raw boolean array for
val(i)and a segment tree for interval summaries. - Store whether each interval is entirely
true. - '
val(i)isO(1)from the array.' - '
find(i)isO(log n)by descending only into intervals that still contain afalse.' - '
change(i)is amortizedO(1)because each tree node can switch fromfalsetotrueonly once.'

