Skip to content

Practice Snapshot Array

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1146
LeetCode Problem

Snapshot Array

Problem Statement

Implement SnapshotArray(length), initially containing length zeros. set(index,val) updates the current value. snap() saves the current array state and returns a zero-based id, increasing by one on every call. get(index,snap_id) returns the value stored at that position in that particular snapshot; later set calls do not alter earlier snapshots.

Source: Snapshot Array on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["SnapshotArray","set","snap","set","get"] [[3],[0,5],[],[0,6],[0,0]]
Output
[null,null,0,null,5]

Constraints

1 <= length <= 5 * 10^4
0 <= index < length
0 <= val <= 10^9
0 <= snap_id < (the total number of times we call snap())
At most 5 * 10^4 calls will be made to set, snap, and get.

Related LeetCode Problems

Practice more problems similar to Snapshot Array.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.