DIY: Snapshot Array

Solve the interview question "Snapshot Array" in this lesson.

Problem statement

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

  • init(length): This property initializes a data structure with length number of indexes. Initially, the value at each index is 0.

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

  • snapshot(obj): This property only takes the struct object as its parameter and returns the snapid. snapid is the number of times that the snapshot() function was called minus 1.

  • get_value(obj,idx,snapid) function returns the value at the index idx with the given snapid.


The input will be two integers, that is an index and a value. We can set the value of the index idx,using the set_value(obj,idx,val) function.

snapshotArr = SnapshotArray.init(3)


The output will be an integer. We will receive an output by calling the get_value(obj,idx,snapid) function. For example, we will get the following output after calling the get_value(obj,idx,snapid) function on the input given above:


Coding exercise

Using the skeleton code given below, you need to implement the SnapshotArray module with the def init(length), set_value(obj,idx,val), snapshot(obj), and get_value(obj,idx,snapid) functions:

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