KatInterview

Versioned Membership Tracker with Point-in-Time Queries

Data Structure DesignAuto-graded · 18 test cases
HardAuto-gradedData Structure Designbinary searchdesignhash map

Last updated

Tests ability to design a copy-on-write set that records per-element change history and answers historical membership queries in O(log n) via binary search.

Overview

You are building a collection that tracks unique integers over time. Instead of copying the whole collection whenever you take a checkpoint, the structure records only what changed and reconstructs past states on demand.

You will process a sequence of named operations driven by a driver function. Each operation is one of the six types described below.

Operations

add(val) Insert val into the collection. Return True if val was absent and was successfully inserted. Return False if val was already present (no change is made).

remove(val) Delete val from the collection. Return True if val was present and was removed. Return False if val was not present (no change is made).

contains(val) Return True if val is currently in the collection, False otherwise.

snapshot() Freeze the current state of the collection as a numbered checkpoint and return its id. The first call returns 0, the second returns 1, and so on. Checkpoint creation must be O(1) — do not copy the entire collection.

contains_at(snapshot_id, val) Return True if val was present in the collection at the moment checkpoint snapshot_id was taken, False otherwise. Raise IndexError if snapshot_id is negative or greater than or equal to the total number of checkpoints taken so far.

Driver Interface

Implement the function:

def versioned_set_ops(operations: List[str], arguments: List[List[int]]) -> List[object]:
  • operations: a list of operation name strings.
  • arguments: parallel list of argument lists. Operations that take no arguments use an empty list [].
  • Return a list containing the return value of each operation in order.

Constraints

  • Total operations: up to 2 × 10^5.
  • Element values: -10^9 ≤ val ≤ 10^9.
  • A valid snapshot_id satisfies 0 ≤ snapshot_id < (number of snapshots taken so far). Anything outside this range must raise IndexError.
  • Multiple add/remove calls on the same element within the same checkpoint interval must not produce redundant history entries — only the final state before the next checkpoint matters.
  • The collection starts empty. No checkpoint exists until the first snapshot() call.

Examples

Example 1

Input: versioned_set_ops(['add', 'add', 'snapshot', 'remove', 'add', 'snapshot', 'contains', 'contains_at', 'contains_at', 'contains_at', 'contains_at'], [[1], [2], [], [1], [3], [], [1], [0, 1], [1, 1], [0, 3], [1, 3]])

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

Example 2

Input: versioned_set_ops(['snapshot', 'contains_at', 'add', 'snapshot', 'remove', 'snapshot', 'contains_at'], [[], [0, 7], [7], [], [7], [], [2, 7]])

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

Hints

Hint 1

Instead of copying the whole collection each checkpoint, think about what minimal information you need to record per element — specifically, the moments when its membership status actually flips.

Hint 2

For each element, maintain a list of (epoch, is_present) pairs in chronological order. A checkpoint ID is just an epoch boundary. To answer a historical query, you need the last recorded event at or before that epoch — binary search is a natural fit.

Hint 3

When multiple mutations affect the same element within the same epoch, only the final state is observable at checkpoint time. Overwrite the last history entry instead of appending when its epoch matches the current one, keeping the list compact.