Snapshot Array
Understand how to implement a Snapshot Array, enabling you to track changes over time with set, snapshot, and get functions. This lesson helps you master a specialized data structure often tested in technical coding interviews.
We'll cover the following...
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 , as we start the count at . The first time this function is called, it saves a snapshot and returns . The time it is called, after saving the snapshot, it returns .
-
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 . After calling the Set Value (1, 4) function, the value of node 1 will change to . If we take a snapshot at this point, the current values of all the nodes will be saved with Snap ID . Now, if we call Set Value (1, 7), the current value for node 1 will change to . Now, if we call the Get Value (1, 0) function, we will get the value of node 1 from snapshot , that is, .
Constraints:
-
length -
idxlength -
val -
snapid(the total number of times we call Snapshot) - At most 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
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)
6
4
4
6
2
3
3
2
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to implement the Snapshot operation.
Try it yourself
Implement your solution in the following coding playground.
Note: The paramaters in “Input #1” field should be interpreted as follows:
- “SnapshotArray”: Constructor
- “set”: Set Value (idx, val)
- “snap”: Snapshot()
- “get” : Get Value (idx, Snap ID)
class SnapshotArray:# Constructordef __init__(self, length):# Write your code herepass# Function set_value sets the value at a given index idx to val.def set_value(self, idx, val):# Write your code herepass# This function takes no parameters and returns the snapid.# snapid is the number of times that the snapshot() function was called minus 1.def snapshot(self):# Replace this placeholder return statement with your codereturn -1# Function get_value returns the value at the index idx with the given snapid.def get_value(self, idx, snapid):# Replace this placeholder return statement with your codereturn -1