A fintech platform uses feature flags to safely roll out UI and risk-model changes to millions of daily active users. During an incident, engineers must answer questions like: “What was the flag state at the exact moment snapshot s was taken?”—even after many later updates.
Design a data structure SnapshotArray that behaves like an integer array, but supports immutable snapshots.
Implement the following API:
SnapshotArray(length: int)
length with all values set to 0.set(index: int, val: int) -> None
index to val in the current (not-yet-snapped) version.snap() -> int
snap_id (starting at 0, increasing by 1).get(index: int, snap_id: int) -> int
index as it was when snapshot snap_id was taken.get must return 0 if an index had never been set at or before that snapshot.set calls to the same index before a snap() should be last-write-wins for that snapshot.1 <= length <= 5 * 10^40 <= index < length0 <= val <= 10^90 <= snap_id < number_of_snaps2 * 10^5 total calls across set, snap, and get