Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-237

Immutable List Prepend

Problem

Simulate an immutable list built by prepending values.

Version 0 is empty. operations contains ["prepend", version, value] (creates a new version and records its id) or ["head", version] (records the first value or None). Prepending never mutates the version it builds on.

Return the list of recorded results.

Hint

Sharing the tail between versions is what makes the list persistent.

Test cases

case 1
in: [["prepend","prepend","head","prepend","head"],[[0,1],[1,2],1,[2,3],3]]
expected: [1,2,1,3,3]
case 2
in: [["head"],[0]]
expected: [null]
case 3
in: [["prepend","prepend","prepend","head"],[[0,1],[1,2],[2,3],3]]
expected: [1,2,3,3]
case 4
in: [["prepend","head"],[[0,9],1]]
expected: [1,9]
Starter codePython
def immutable_prepend(operations, values):
    # Your code here
    pass

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