KatInterview

Spaced Points Stream: Cancel a Nearby Point or Insert

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designordered-setbalanced-bstbinary-search

Last updated

Maintain a dynamic ordered set of numbers and, per arrival, find the smallest stored value in a closed window around it, deleting it or inserting the newcomer.

Spaced Points Stream

A set of real numbers on a number line starts out empty. Numbers arrive one at a time, and each arrival either cancels a number already in the set or is stored itself.

You are given a distance threshold d, a list of operation names operations, and a parallel list arguments. Entry arguments[i] is a list holding the arguments of operations[i].

Every operation name is "process", and its argument list holds a single number p. Handle process(p) as follows:

  1. Look at the stored numbers q that lie in the closed window p - d <= q <= p + d. Compute both bounds as ordinary floating-point values, and use inclusive comparisons.
  2. If at least one stored number is in the window, remove the smallest such number from the set, do not store p, and record False.
  3. Otherwise, store p and record True.

Return the list of recorded booleans, one per operation, in order.

If an operation name is anything other than "process", raise a ValueError.

Clarifications

  • Equal values are at distance 0, so for d >= 0 a value equal to a stored one cancels it.
  • If d is negative, the window is empty, so every call stores its point and returns True. Duplicate values can then be stored, and each stored copy counts separately.
  • When several stored numbers fall in the window, always remove the smallest one. This fixed rule makes the output unique.

Constraints

  • Up to 10^5 operations.
  • len(operations) == len(arguments), and each argument list has exactly one number.
  • Points and d are floating-point numbers (integers may appear as well).
  • Each operation should run in better than O(N) time, so a full scan of the stored points per call is too slow. Target O(log N) per operation.

Examples

Example 1

Input: track_spaced_points(2, ['process', 'process', 'process', 'process'], [[5], [6], [10], [12]])

Output: [True, False, True, False]

Example 2

Input: track_spaced_points(2, ['process', 'process', 'process', 'process', 'process'], [[5], [6], [10], [12], [11]])

Output: [True, False, True, False, True]

Example 3

Input: track_spaced_points(0, ['process', 'process', 'process', 'process'], [[3.5], [3.5], [3.5], [4]])

Output: [True, False, True, True]

Hints

Hint 1

Every stored number that could cancel the new point lies in the closed window [p - d, p + d]. You do not need to list them all, only the smallest one.

Hint 2

Find the smallest stored value that is >= p - d (a lower bound). If it exists and is <= p + d, it is the smallest value in the window. Otherwise the window is empty.

Hint 3

You need an ordered set with insert, delete, and lower-bound all in O(log n). A plain sorted list with insertion is O(n) per update, so use a balanced BST such as an AVL tree, or a sorted-container library if allowed.