Skip to content
DeepForgeby svx
All problems
HardData Structuresds-126

Snapshot Array Set and Get

Problem

Simulate a snapshot array that supports point writes and historical lookups.

operations contains "set" (indices[i] = values[i]), "snap" (returns the current snapshot id and then increments it), or "get" (indices[i] is the position and values[i] is the snapshot id). get returns the value of that position at the time of the given snapshot, or 0 if it had never been set.

Return the list of recorded results.

Hint

Record (snapshot id, value) on every set and scan the history for the largest id not exceeding the query.

Test cases

case 1
in: [["set","snap","get","set","get"],[0,0,0,0,0],[5,0,0,6,0]]
expected: [null,0,5,null,5]
case 2
in: [["set","snap","set","snap","get","get"],[0,0,1,1,1,0],[1,0,2,0,0,1]]
expected: [null,0,null,1,0,1]
case 3
in: [["get"],[0],[0]]
expected: [0]
case 4
in: [["snap","snap","get"],[0,0,2],[0,0,0]]
expected: [0,1,0]
Starter codePython
def snapshot_array(operations, indices, values):
    # Your code here
    pass

4 test cases · run your code against them in the editor