Google questions

Range Distinctness Check

GoogleOnsiteMedium

You are given an integer array nums that never changes. RangeUnique(nums) builds a checker once, and each query asks about a range of positions counted from 0, with both ends included.

Part 1: Constant-time range queries

Implement a checker for range distinctness. is_unique(l, r) returns true when all the values nums[l] through nums[r] are different from one another, and false when some value appears twice or more in that range. A range of one element is always unique.

The queries are many, so the constructor may take linear time, but each call to is_unique must take constant time.

  1. Example 1
    init
    [[3,1,4,1,5,9,2,6]]
    operations
    [["is_unique",[0,7]],["is_unique",[0,2]],["is_unique",[1,3]],["is_unique",[2,5]],["is_unique",[3,3]],["is_unique",[1,4]],["is_unique",[3,7]]]
    Output
    [false,true,false,true,true,false,true]

    Why: The value 1 sits at indices 1 and 3. Any range containing both fails, such as (0, 7), (1, 3) and (1, 4); (0, 2), (2, 5), (3, 3) and (3, 7) do not.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= len(operations) <= 10^5
  • 0 <= l <= r < len(nums)
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Range Distinctness Check: Google interview question | LoopCoach