Search⌘ K
AI Features

Snapshot Array

Explore how to implement a snapshot array data structure that supports setting values, capturing snapshots to save states with unique IDs, and retrieving historical values from those snapshots. Understand the constructor, set, snapshot, and get methods to manage and query versions of an array effectively. This lesson helps you master a custom data structure relevant for complex coding problems and interviews.

Statement

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

  • Constructor (length): This is the constructor and it 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 11, as we start the count at 00. The first time this function is called, it saves a snapshot and returns 00. The nthn^{th} time it is called, after saving the snapshot, it returns n1n-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 00. After calling the Set Value (1, 4) function, the value of node 1 will change to 44. If we take a snapshot at this point, the current values of all the nodes will be saved with Snap ID 00. Now, if we call Set Value (1, 7), the current value for node 1 will change to 77. Now, if we call the Get Value (1, 0) function, we will get the value of node 1 from snapshot 00, that is, 44.

Constraints:

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

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Snapshot Array

1.

Select the correct output for the given code:

snapshot array = Constructor(3)
snapshot array.set value(0, 4)
snapshot array.snapshot()
snapshot array.get value(0, 0)
snapshot array.set value(1, 6) 
snapshot array.snapshot()
snapshot array.get value(1, 1)  
A.

6

4

B.

4

6

C.

2

3

D.

3

2


1 / 2

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3

Try it yourself

Implement your solution in the following coding playground.

Java
usercode > SnapshotArray.java
import java.util.*;
class SnapshotArray {
// Constructor
public SnapshotArray(int length) {
// Write your code here
}
// Function setValue sets the value at a given index idx to val.
public void setValue(int idx, int state) {
// Write your code here
}
// This function takes no parameters and returns the snapid.
// snapid is the number of times that the snapshot() function was called minus 1.
public int snapshot() {
// Replace this placeholder return statement with your code
return -1;
}
// Function getValue returns the value at the index idx with the given snapid.
public int getValue(int idx, int snapshotId1) {
// Replace this placeholder return statement with your code
return -1;
}
}
Snapshot Array