Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-235

Persistent Stack Versions

Problem

Simulate persistent stack versions.

Version 0 is empty. operations contains ["push", version, value], ["pop", version], or ["top", version]. push and pop create a new version and record its id; top records the top value or None. Earlier versions must never change.

Return the list of recorded results.

Hint

Copy the referenced version before mutating it so old versions stay intact.

Test cases

case 1
in: [["push","push","top","pop","top","top"],[[0,"a"],[1,"b"],1,2,1,3]]
expected: [1,2,"a",3,"a","a"]
case 2
in: [["top"],[0]]
expected: [null]
case 3
in: [["pop"],[0]]
expected: [1]
case 4
in: [["push","top"],[[0,"x"],1]]
expected: [1,"x"]
Starter codePython
def persistent_stack_ops(operations, values):
    # Your code here
    pass

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