# Solution: Snapshot Array

Let's solve the Snapshot Array problem using the Custom Data Structures pattern.

We'll cover the following

## Statement

In this challenge, you have to implement a Snapshot Array with the following properties:

• NewSnapshotArray (length): Initializes the data structure to hold the specified number of indexes.

• Set Value (idx, val): This property sets the value at a given index idx to value val.

• Snapshot(): This method takes no parameters and returns the Snap ID. Snap ID is the number of times that the snapshot function was called, less $1$, as we start the count at $0$. The first time this function is called, it saves a snapshot and returns $0$. The $n^{th}$ time it is called, after saving the snapshot, it returns $n-1$.

• Get Value (idx, Snap ID) method returns the value at the index in the snapshot with the given Snap ID.

Suppose that we have three nodes whose values we wish to track in the snapshot array. Initially, the value of all the nodes will be $0$. After calling the Set Value (1, 4) function, the value of node 1 will change to $4$. If we take a snapshot at this point, the current values of all the nodes will be saved with Snap ID $0$. Now, if we call Set Value (1, 7), the current value for node 1 will change to $7$. Now, if we call the Get Value (1, 0) function, we will get the value of node 1 from snapshot $0$, that is, $4$.

Constraints:

• $1 \leq$ length $\leq 1000$
• $0 \leq$ idx $<$ length
• $0 \leq$ val $\leq 10^9$
• $0 \leq$ snapid $<$ (the total number of times we call Snapshot)
• At most $5 \times 10^3$ calls will be made to Set Value, Snapshot, and Get Value.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.