FastPrepBatch Point-in-Range Queries

Batch Point-in-Range Queries

Google logoGoogle● MediumINTERNPHONE SCREEN
Learn

Problem statement

You are given closed numeric ranges in ranges and query values in points. Each range is [left, right] and includes both endpoints.

Return a boolean array where result i is true exactly when points[i] lies in at least one range.

Ranges may overlap or touch, and all inputs may contain fractional values. Compare the supplied finite values directly; do not apply an epsilon.

Function

pointsInRanges(ranges: double[][], points: double[]) → boolean[]

Examples

Example 1

ranges = [[1.5,3.0],[5.0,7.25]]points = [1.5,4.0,7.25]return = [true,false,true]

The first and third values equal closed endpoints, while 4.0 lies in the gap.

Example 2

ranges = [[0.0,2.0],[1.0,4.0],[-2.0,-1.0]]points = [-1.0,0.0,3.5,4.1]return = [true,true,true,false]

The overlapping first two ranges cover every value from 0.0 through 4.0. The negative range includes -1.0.

Example 3

ranges = []points = [0.0,-2.5]return = [false,false]

No point is covered when there are no ranges.

Constraints

  • 0 <= ranges.length, points.length <= 200000.
  • Every range contains exactly two finite values and satisfies ranges[i][0] <= ranges[i][1].
  • Every point is finite.
  • Every endpoint and point is between -10^12 and 10^12.

More Google problems

See Google hiring insights
public boolean[] pointsInRanges(double[][] ranges, double[] points) {
    // Write your code here.
}
ranges[[1.5,3.0],[5.0,7.25]]
points[1.5,4.0,7.25]
expected[true,false,true]
Checking account…