KatInterview

Frozen-View Integer Set with Independent Cursors

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designdesignhash-setiterator

Last updated

Simulate a mutable integer set whose iterators capture the set's contents at creation time and stay isolated from later changes and from each other.

Frozen-View Integer Set

You are simulating a mutable set of integers that can hand out iterators. Each iterator shows the set exactly as it was at the moment the iterator was created. Later changes to the set, and the progress of other iterators, never affect it.

You are given two lists of equal length. operations[i] is the name of the i-th command and arguments[i] is the list of arguments for that command. Process the commands in order. Every command appends exactly one entry to the output list.

OperationArgumentsEffectOutput entry
add[value]Insert value into the set. Adding a value already present changes nothing.None
remove[value]Delete value from the set. Removing a value that is not present changes nothing.None
contains[value]Check whether value is currently in the set.True or False
iterator[]Create a new iterator that captures the current contents of the set.The iterator's id: 0 for the first iterator created, 1 for the second, and so on
hasNext[iterator_id]Check whether the iterator still has elements left to return.True or False
next[iterator_id]Return the next element of the iterator and advance it. If the iterator is exhausted, return None and do not advance.The element, or None

Iteration order

An iterator returns the elements of its captured contents in ascending numeric order, each exactly once.

Details

  • Each iterator has its own position. Advancing one iterator never moves another, even if they were created when the set had identical contents.
  • An iterator is unaffected by any add or remove performed after it was created, including those that happen midway through its traversal.
  • An iterator created on an empty set has hasNext equal to False and next equal to None.
  • Repeated next calls on an exhausted iterator keep returning None.
  • Iterator ids passed to hasNext and next always refer to iterators that have already been created.

Return the list of output entries, one per command, in order.

Constraints

  • len(operations) == len(arguments)
  • The set holds at most 10^5 elements at any time.
  • Values are integers.
  • operation is always one of add, remove, contains, iterator, hasNext, next.
  • add, remove, and contains should run in O(1) average or O(log N) time.

Examples

Example 1

Input: simulate_frozen_set(['add', 'add', 'iterator', 'add', 'remove', 'next', 'next', 'hasNext'], [[1], [2], [], [3], [1], [0], [0], [0]])

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

Example 2

Input: simulate_frozen_set(['iterator', 'hasNext', 'next', 'hasNext'], [[], [0], [0], [0]])

Output: [0, False, None, False]

Example 3

Input: simulate_frozen_set(['add', 'add', 'iterator', 'iterator', 'remove', 'next', 'next', 'next', 'next'], [[5], [1], [], [], [5], [0], [1], [0], [1]])

Output: [None, None, 0, 1, None, 1, 1, 5, 5]

Hints

Hint 1

Keep two separate pieces of state: a live set for add, remove, and contains, and a per-iterator record for traversal.

Hint 2

When an iterator is created, copy the current contents into a sorted list and pair it with a cursor index. Later changes touch only the live set, so the copy never changes.

Hint 3

Use discard rather than remove on a Python set so that removing a missing value is a no-op, and only advance the cursor in next when elements remain.