FastPrepUnique Elements in Range Queries
Problems/Unique Elements in Range Queries

Unique Elements in Range Queries

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report asks whether a range contains only unique elements and explicitly requests preprocessing for constant-time repeated queries. The batch runner exposes that complete follow-up.

For every inclusive query [left, right], determine whether all values in nums[left..right] are unique.

Return one boolean per query. Preprocess the array so each query is answered in O(1) time.

Function

uniqueRangeQueries(nums: int[], queries: int[][]) → boolean[]

Examples

Example 1

nums = [1,2,3,1]queries = [[0,2],[1,3],[0,3],[2,2]]return = [true,true,false,true]

Only the full range contains both occurrences of 1.

Example 2

nums = [5,5,5]queries = [[0,0],[0,1],[1,2]]return = [true,false,false]

Single-element ranges are unique; every longer range repeats 5.

Example 3

nums = [4,3,2,1]queries = [[0,3],[1,2]]return = [true,true]

The array contains no duplicate values.

Constraints

  • 1 <= nums.length <= 200000.
  • -10^9 <= nums[i] <= 10^9.
  • 1 <= queries.length <= 200000.
  • Every query has two indices satisfying 0 <= left <= right < nums.length.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public boolean[] uniqueRangeQueries(int[] nums, int[][] queries) {
        // Write your solution here.
    }
}
Checking account…