Snapshot Array for Versioned Privacy Settings
Last updated: November 29, 2026
Quick Overview
Design an array of integer settings that supports set(index, value), snap() returning a snapshot id, and get(index, snap_id) returning the value at that snapshot. Snapshots must be cheap even when the array is large and few values change.
Meta
November 29, 20260
0
0 solved
Design an array of integer settings that supports set(index, value), snap() returning a snapshot id, and get(index, snap_id) returning the value at that snapshot. Snapshots must be cheap even when the array is large and few values change.
Meta keeps auditable history for privacy and audience settings, so reading a setting 'as of' an earlier version is a natural framing. The interviewer checks that you avoid copying the array on snap, and that you use per-index version lists with binary search.
What the Interviewer Expects
- Reject copying the whole array per snapshot
- Store per-index history as (snap_id, value) pairs
- Overwrite in place when setting twice within the same snapshot
- Use binary search to answer historical reads
- Discuss memory proportional to changes, not snapshots
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you garbage-collect snapshots nobody can read anymore?
- What if reads mostly target the latest snapshot?
- How would you persist this to disk for an audit log?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
setwrites into the current (unsnapped) version.snap()freezes the current version and returns its id (0, 1, 2, ...).get(i, s)returns the value of indexias of snapshots; unset va...
High-Level Design
For each index keep two parallel lists: the snapshot ids at which it changed and the values. Initialize with (0, 0). A set during snapshot k either overwrites the last entry if it already belong...