KatInterview

Time-Travel Array with Historical Query Support

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designbinary-searchversioningdata-structures

Last updated

Design a data structure that tracks an array's evolution through snapshots and answers queries about past states efficiently.

Implement a time-traveling integer array that maintains a complete history through snapshots. The array has a fixed size and all elements begin at zero. You'll process three types of operations: modifying values in the current state, capturing snapshots, and retrieving values from previous snapshots.

Your task is to process a sequence of operations and return the results of snapshot and query operations.

Function Signature

def process_versioned_array(size: int, operations: List[str], parameters: List[List[int]]) -> List[int]:

Operation Types

The operations and parameters lists describe the sequence of commands to execute:

  • "set" with parameters[i] = [index, value]: Update the element at position index to value in the current array state. Produces no output.

  • "snap" with parameters[i] = []: Create a numbered snapshot of the current array state and return the snapshot identifier. Snapshot IDs start at 0 and increment by 1 each time a snapshot is created.

  • "get" with parameters[i] = [index, snap_id]: Return the value stored at position index when snapshot snap_id was created.

Behavior Rules

  • Snapshots capture the array state at the moment they are created, including all preceding "set" operations but excluding any subsequent ones.
  • If a position is modified multiple times between consecutive snapshots, only the most recent value appears in the next snapshot.
  • Any array position that has never been set reads as 0 in all snapshots.
  • Return a list containing all outputs from "snap" and "get" operations, in the order they occur.

Constraints

  • 1 <= size <= 5 * 10^4
  • 1 <= len(operations) == len(parameters) <= 5 * 10^4
  • Each operations[i] is either "set", "snap", or "get"
  • For "set" operations: 0 <= index < size and 0 <= value <= 10^9
  • For "get" operations: 0 <= index < size and 0 <= snap_id < total_snapshots, where total_snapshots is the count of "snap" operations before this "get"

Examples

Example 1

Input: process_versioned_array(3, ['set', 'snap', 'set', 'get'], [[0, 5], [], [0, 6], [0, 0]])

Output: [0, 5]

Example 2

Input: process_versioned_array(2, ['snap', 'snap', 'get', 'set', 'get', 'snap', 'get', 'get'], [[], [], [1, 1], [1, 4], [1, 1], [], [1, 2], [0, 2]])

Output: [0, 1, 0, 0, 2, 4, 0]

Example 3

Input: process_versioned_array(1, ['set', 'set', 'snap', 'set', 'get'], [[0, 3], [0, 7], [], [0, 9], [0, 0]])

Output: [0, 7]

Hints

Hint 1

Storing a full array copy for each snapshot is too slow—think about what information actually changes between snapshots.

Hint 2

Track modifications on a per-index basis. Each array position can maintain its own history of changes tagged with version numbers.

Hint 3

When searching through a position's history for a specific snapshot, you need the most recent change that occurred at or before that snapshot—binary search can help.