Snapshot Array
Implement a SnapshotArray that supports the following interface:
SnapshotArray(int length)initializes an array-like data structure with the given length. Initially, each element equals 0.void set(index, val)sets the element at the givenindexto be equal toval.int snap()takes a snapshot of the array and returns thesnap_id: the total number of times we calledsnap()minus1.int get(index, snap_id)returns the value at the givenindex, at the time we took the snapshot with the givensnap_id
Example 1:
Input: ["SnapshotArray","set","snap","set","get"]
[[3],[0,5],[],[0,6],[0,0]]
Output: [null,null,0,null,5]
Explanation:
SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
snapshotArr.set(0,5); // Set array[0] = 5
snapshotArr.snap(); // Take a snapshot, return snap_id = 0
snapshotArr.set(0,6);
snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5
Constraints:
1 <= length <= 5 * 1040 <= index < length0 <= val <= 1090 <= snap_id <(the total number of times we callsnap())- At most
5 * 104calls will be made toset,snap, andget.
Solution
We wish to find the pos for the most recent value at the time we took the snapshot with the given snap_id,
we are trying to find the rightmost index in history=histories[i] such that the snap_id at history[pos] is less or equal to the target snap_id (a[i][0] <= snap_id).
This means that the feasible function is a[i][0] <= snap_id, whenever this is true, we must check the positions on its right to find the rightmost position that makes this condition hold.

Implementation
class SnapshotArray(object):
def __init__(self, n):
# set up histories so that each index has its own history
self.histories = [[[-1, 0]] for _ in range(n)]
self.snap_id = 0
def set(self, index, val):
self.histories[index].append([self.snap_id, val])
def snap(self):
self.snap_id += 1
return self.snap_id - 1
def get(self, index, snap_id):
left, right, pos = 0, len(self.histories[index])-1, -1
while left <= right:
mid = (left+right) // 2
if self.histories[index][mid][0] <= snap_id:
left = mid + 1
pos = mid
else:
right = mid - 1
return self.histories[index][pos][1]
Intuition
Instead of copying the entire array each time we take a snapshot, we wish to only store the changes to each index.
we keep track of an array histories of size n where histories[i] is an array
that stores the history of the changes of array[i]'s values.
We use the pair (snap_id, value) to indicate that we have updated array[i]=value at the time we took the snapshot with the given snap_id.
So when implementing get(snap_id) for index i, we will do binary search on histories[i] to find the index pos in histories[i] that contains
the most recent value up to the time we took the snapshot with the given snap_id.
Ready to land your dream job?
Unlock your dream job with a 5-minute evaluator for a personalized learning plan!
Start EvaluatorThe three-steps of Depth First Search are:
- Identify states;
- Draw the state-space tree;
- DFS on the state-space tree.
Recommended Readings
Coding Interview Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way
Recursion If you prefer videos here's a video that explains recursion in a fun and easy way Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https assets algo monster recursion jpg You first call Ben and ask him
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Want a Structured Path to Master System Design Too? Don’t Miss This!