Snapshot Array
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Build an array of integers that can be frozen into numbered snapshots, where old snapshots can still be read after later writes.
Implement:
- SnapshotArray(length) creates an array of length elements, all 0.
- set(index, val) writes val at index in the live array.
- snap() takes a snapshot and returns its id. Ids start at 0 and increase by one with each call.
- get(index, snap_id) returns the value at index as it was when snapshot snap_id was taken.
get is only called with a snap_id that has already been returned by snap. Copying the whole array on every snap is too slow for the limits: store only changes.
Examples
Constraints
- 1 <= length <= 5 * 10^4
- 0 <= index < length
- 0 <= val <= 10^9
- 0 <= snap_id < (number of `snap` calls so far)
- At most 5 * 10^4 calls in total.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.