KatInterview

Closest Repeat Distance with Fast Threshold Queries

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designhash-mappreprocessingbatch-queries

Last updated

Precompute the smallest distance between equal values in an array once, then answer many "repeat within k" and "closest repeat" queries in constant time each.

Closest Repeat Queries

You are given an integer array nums that never changes, together with a list of queries to run against it. Because many queries are issued against the same array, the array should be analysed once, and each query should then be answered in constant time.

Two positions i and j form a repeat pair if i != j and nums[i] == nums[j]. The gap of such a pair is abs(i - j).

Query types

Each query is identified by a name in operations, with its parameters in the matching entry of arguments:

  • "min_gap" (arguments: an empty list): return the smallest gap over all repeat pairs in nums. If no value occurs more than once, return -1.
  • "has_duplicate_within" (arguments: [k]): return true if at least one repeat pair has gap less than or equal to k, otherwise false. If the array has no repeat pair, the answer is false for every k. A k of 0 or less always yields false, since every real gap is at least 1.

Function

Implement closest_repeat_queries(nums, operations, arguments). It returns a list with one result per query, in the same order as operations. operations[t] pairs with arguments[t]. Integer results are returned for "min_gap" and booleans for "has_duplicate_within".

Any operation name other than the two above should raise a ValueError.

Constraints

  • 1 <= len(nums), and the array can be large (on the order of 10^5 elements).
  • Elements of nums are integers.
  • len(operations) == len(arguments), and there can be up to about 10^5 queries.
  • k is an integer and may be zero, negative, or larger than the array length.
  • Preprocessing should take O(n) expected time and at most O(n) extra space; each query should take O(1) time.

Examples

Example 1

Input: closest_repeat_queries([1, 2, 3, 1, 2, 3], ['min_gap', 'has_duplicate_within', 'has_duplicate_within', 'has_duplicate_within'], [[], [2], [3], [5]])

Output: [3, False, True, True]

Example 2

Input: closest_repeat_queries([10, 20, 30, 40], ['min_gap', 'has_duplicate_within'], [[], [10]])

Output: [-1, False]

Example 3

Input: closest_repeat_queries([7, 7, 7, 8, 9, 8], ['has_duplicate_within', 'min_gap', 'has_duplicate_within', 'has_duplicate_within'], [[0], [], [1], [2]])

Output: [False, 1, True, True]

Hints

Hint 1

Whether a repeat pair with gap at most k exists depends on a single number: the smallest gap between any two equal values. Compute it once.

Hint 2

You do not need to compare all pairs. For each value, it is enough to compare each occurrence with the previous occurrence of the same value, so a dictionary from value to latest index suffices.

Hint 3

Keep the 'no repeats' case separate from real gaps (for example with None). Reporting -1 for min_gap is fine, but do not compare that -1 against k.