Version Warehouse Bin Quantities With Cheap Snapshots
Last updated: November 29, 2026
Quick Overview
Design an array of bin quantities with `set(index, value)`, `snap()` returning a snapshot id, and `get(index, snap_id)` returning the value at that index when that snapshot was taken. Snapshots must not copy the whole array.
Amazon
November 29, 20260
0
0 solved
Design an array of bin quantities with `set(index, value)`, `snap()` returning a snapshot id, and `get(index, snap_id)` returning the value at that index when that snapshot was taken. Snapshots must not copy the whole array.
An onsite design-a-structure question in Amazon's style, framed around auditing inventory counts at the end of each cycle count without copying millions of bins. The interviewer wants per-index version history with binary search and a clear memory argument.
What the Interviewer Expects
- Rejects full-copy snapshots as O(n) per snap
- Stores per-index lists of (snap_id, value) changes
- Overwrites the last entry when set is called twice in one snapshot
- Uses binary search for get
- States memory as O(number of sets plus n)
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 will read again?
- How would you persist this to disk so it survives restarts?
- How would you support get_range(l, r, snap_id) 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
n bins start at 0. snap() freezes the current state and returns ids 0, 1, 2, and so on. get(i, s) returns bin i as of snapshot s. Up to calls, so copying the array per snapsho...
High-Level Design
Store changes, not states. For each bin keep a list of (snap_id, value) pairs sorted by snap_id, where snap_id is the id of the snapshot that the next snap() call will return. Repeated sets wi...