Versioned Settings Array with Cheap Snapshots and Historical Reads
Last updated: November 29, 2026
Quick Overview
Implement an array of length n supporting set(index, value), snap() which returns a snapshot id, and get(index, snap_id) which returns the value at that index when that snapshot was taken.
0
0
0 solved
Implement an array of length n supporting set(index, value), snap() which returns a snapshot id, and get(index, snap_id) which returns the value at that index when that snapshot was taken.
Framed around versioned configuration in systems like Google's settings or Docs revision history, this tests whether you avoid copying the whole array per snapshot. The interviewer is looking for per-index version lists and a binary search on reads.
What the Interviewer Expects
- Rejects full copies per snap as O(n) per snapshot
- Stores per index a list of (snap_id, value) pairs
- Overwrites in place when multiple sets happen within one snap
- Uses binary search to answer reads in O(log k)
- Explains memory as proportional to the number of sets
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
- What if reads at the latest snapshot are far more common than historical reads?
- How would you persist this to disk and support deleting old snapshots?
- Support a diff(snap_a, snap_b) that lists changed indices efficiently.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
SnapshotArray(n): all values start at 0.set(i, v): write to the current (unsnapped) version.snap(): freeze and return the id (0, 1, 2, ...).get(i, snap_id): value at i as of that sn...
High-Level Design
For each index keep a list of (snap_id, value) pairs, sorted by snap_id because ids only increase. set appends (current_id, v), or overwrites the last pair if it already belongs to the current i...