Persistent Data Structure - Problem
A persistent data structure preserves all previous versions of itself when modified. When you update a persistent array, the original version remains unchanged and accessible, while a new version is created with the modification.
Implement a PersistentArray class that supports:
set(index, value)- Returns a new version with the element atindexset tovalueget(index)- Returns the element atindexin the current versionsize()- Returns the size of the array
Given a sequence of operations, return the final values of all versions created during the process.
Input & Output
Example 1 — Basic Persistent Operations
$
Input:
initial_array = [1,2,3], operations = [["set", 1, 9]]
›
Output:
[[1,2,3], [1,9,3]]
💡 Note:
Start with [1,2,3] as Version 1. After set(1,9), create Version 2 as [1,9,3]. Both versions remain accessible.
Example 2 — Multiple Versions
$
Input:
initial_array = [5,10], operations = [["set", 0, 7], ["set", 1, 15]]
›
Output:
[[5,10], [7,10], [7,15]]
💡 Note:
Version 1: [5,10] → Version 2: [7,10] after set(0,7) → Version 3: [7,15] after set(1,15)
Example 3 — Single Element Array
$
Input:
initial_array = [42], operations = [["set", 0, 99]]
›
Output:
[[42], [99]]
💡 Note:
Minimal case: original [42] and modified [99] version both stored
Constraints
- 1 ≤ initial_array.length ≤ 1000
- 1 ≤ operations.length ≤ 100
- -106 ≤ array elements ≤ 106
- Each operation is ["set", index, value] where 0 ≤ index < array.length
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code