KatInterview

Point-in-Time Set Views from an Operation Log

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designdesignhash-setsnapshot

Last updated

Simulate a set that processes add, remove, contains and snapshot operations, where each snapshot must be an independent frozen copy of the set at that moment.

You are simulating a set of integers that can hand out frozen, point-in-time views of itself.

The set starts empty. You receive two parallel lists, operations and arguments. The operation at index i is operations[i], and its parameters are arguments[i]. Apply the operations in order. Each one produces exactly one entry in the output list.

Operations

OperationArgumentsEffectOutput entry
"add"[x]Insert x if it is not already in the set.None
"remove"[x]Delete x if it is in the set. Otherwise do nothing.None
"contains"[x]Check whether x is currently in the set.True or False
"iterator"[]Take a snapshot of the set as it is right now.A list of the snapshot's values

Snapshot rules

  • A snapshot holds exactly the values that were in the set when the "iterator" operation was processed, each one once.
  • Later "add" and "remove" operations must never change a snapshot that was already produced.
  • Snapshots are independent of each other and of the live set.
  • An empty set produces [].

Order of values inside a snapshot

Conceptually the order of a snapshot does not matter, but the output is compared as a plain list, so the order must be reproduced exactly. Keep the live values in a built-in Python set, updated with set.add for "add" and set.discard for "remove" in operation order. A snapshot is the result of list(live_set) taken at that moment. Do not sort the values.

Task

Return the list of output entries, one per operation, in the same order as operations.

Constraints

  • len(operations) == len(arguments), and both lists have between 0 and 10^4 entries.
  • Every operation name is one of "add", "remove", "contains", "iterator".
  • "add", "remove" and "contains" have exactly one integer argument. "iterator" has an empty argument list.
  • Integer arguments fit in a signed 32-bit integer.
  • Adding a value that is already present, or removing one that is absent, is a no-op and is not an error.

Examples

Example 1

Input: run_snapshot_set_ops(['add', 'add', 'iterator', 'remove', 'add', 'iterator'], [[1], [2], [], [1], [3], []])

Output: [None, None, [1, 2], None, None, [2, 3]]

Example 2

Input: run_snapshot_set_ops(['add', 'contains', 'remove', 'contains', 'iterator'], [[5], [5], [9], [9], []])

Output: [None, True, None, False, [5]]

Hints

Hint 1

Start with the simplest correct model: keep a hash set for the live values and decide what an 'iterator' operation should record.

Hint 2

A snapshot must not share mutable state with the live set. Think about what you must copy, and when, so later adds and removes cannot leak in.

Hint 3

Build each snapshot as a brand-new list from the live set at that moment. Do not reorder it, because the expected output uses the set's own iteration order. Copying costs O(n) per snapshot; versioning each stored occurrence with an (added, removed) range is the idea for O(1) snapshot creation.