KatInterview

Simulate Snapshot Set Operations and Iterators

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designdesign-dsarraysdata-structures

Last updated

Simulate mutation and snapshot iteration on a persistent set structure using interval-based logical timestamps.

You are tasked with evaluating a data structure that implements a set of integers supporting $O(1)$ snapshot iterators.

The set maintains state across operations and supports:

  • add(n): Inserts integer n if it is not currently present. Returns True if added, or False if n was already present.
  • remove(n): Deletes integer n if it is currently present. Returns True if removed, or False if n was not present.
  • contains(n): Returns True if n is currently in the set, or False otherwise.
  • getIterator(): Creates and returns a snapshot iterator freezing the exact set contents at the time of calling.

Behavioral & Snapshot Rules

  1. Logical Timestamping: A logical clock starts at $0$. Every successful modification (add or remove) increments the clock by $1$. Operations that fail (e.g., adding an existing element or removing a missing element) do not increment the clock.
  2. Lifespan Intervals: Each successful insertion creates a record [value, insertion_clock, deletion_clock]. The deletion_clock is initially None. When an element is successfully removed, its active record's deletion_clock is updated to the new clock value. If a previously removed element is re-added later, a brand new record is appended to history.
  3. Snapshot Visibility: An iterator instantiated when the logical clock is $T$ views elements whose records satisfy: $$\text{insertion_clock} \le T \quad \text{and} \quad (\text{deletion_clock is None} \lor T < \text{deletion_clock})$$
  4. Iteration Order: Iterators traverse the set's historical insertion record array sequentially from index $0$ onward, skipping elements that fail the visibility condition for timestamp $T$.
  5. Iterator Independence: Iterators are independent and maintain their own index pointer into the set's history array. Modifications occurring after an iterator's creation do not affect its output.

Function Task

Implement snapshot_set_operations(operations, arguments) to process a list of commands and return a list of their respective results.

Supported operations in operations and their argument lists arguments:

  • "SnapshotSet": Arguments []. Initializes a new empty set and clears all stored iterators. Appends None (rendered as null) to the output.
  • "add": Arguments [n]. Calls add(n). Appends the return boolean to the output.
  • "remove": Arguments [n]. Calls remove(n). Appends the return boolean to the output.
  • "contains": Arguments [n]. Calls contains(n). Appends the return boolean to the output.
  • "getIterator": Arguments []. Spawns a new iterator via getIterator(). Stores it in a dictionary indexed by incremental integer handles starting at 0 (0, 1, 2, ...). Appends the integer handle to the output.
  • "hasNext": Arguments [handle]. Calls hasNext() on the iterator handle. Appends the boolean result to the output.
  • "next": Arguments [handle]. Calls next() on the iterator handle. Appends the integer element, or None if StopIteration is raised.
  • "iterate": Arguments [handle]. Consumes all remaining elements from the iterator handle into a list and appends that list to the output.

Constraints

  • $1 \le \text{len}(operations) \le 10^5$
  • len(operations) == len(arguments)
  • Element values fit in standard signed 32-bit integers.

Examples

Example 1

Input: snapshot_set_operations(['SnapshotSet', 'add', 'add', 'add', 'getIterator', 'remove', 'add', 'contains', 'iterate'], [[], [1], [2], [1], [], [1], [3], [1], [0]])

Output: [None, True, True, False, 0, True, True, False, [1, 2]]

Example 2

Input: snapshot_set_operations(['SnapshotSet', 'add', 'add', 'getIterator', 'getIterator', 'next', 'add', 'remove', 'iterate', 'iterate'], [[], [10], [20], [], [], [0], [30], [10], [0], [1]])

Output: [None, True, True, 0, 1, 10, True, True, [20], [10, 20]]

Example 3

Input: snapshot_set_operations(['SnapshotSet', 'getIterator', 'hasNext', 'next'], [[], [], [0], [0]])

Output: [None, 0, False, None]

Hints

Hint 1

Maintain a global logical timestamp (clock) that increments only on successful additions and deletions.

Hint 2

Track history entries as list elements of [value, insertion_time, deletion_time]. Store active entries in a key-to-entry map.

Hint 3

An iterator captured at timestamp T inspects history entries sequentially: an entry is valid if insertion_time <= T and (deletion_time is None or T < deletion_time).