Percentile and Boundary Queries
Problem statement
A calibration table pairs each numeric boundaries[i] with percentiles[i]. Between adjacent pairs, the relationship is linear. This table is the full model for this exercise; no raw samples or bucket counts are supplied.
Answer every request using the same table:
- If
inverseisfalse, each request is a percentile and you return its numeric boundary value. - If
inverseistrue, each request is a numeric value and you return its percentile.
At a table anchor, return the paired value exactly. Between two anchors, use the same fraction of the interval on the other axis. Clamp a request below the input axis range to its first paired value, and one above the range to its last paired value. Preserve request order. An empty request array returns an empty result.
This is a piecewise-linear calibrated percentile model, rather than a nearest-rank percentile over raw samples. Its interpolation, inverse and endpoint rules are specified here for practice.
Function
percentileBoundaryQueries(boundaries: double[], percentiles: double[], requests: double[], inverse: boolean) → double[]Examples
Example 1
boundaries = [0,10,30]percentiles = [0,50,100]requests = [25,50,75,0,100]inverse = falsereturn = [5,10,20,0,30]Percentile 25 is halfway between 0 and 50, so its value is halfway between 0 and 10: 5. The anchor 50 maps to 10. Percentile 75 maps to 20. Endpoints map to 0 and 30.
Example 2
boundaries = [0,10,30]percentiles = [0,50,100]requests = [5,10,20,-5,40]inverse = truereturn = [25,50,75,0,100]Value 5 is halfway through the first interval, giving percentile 25. Value 10 is the 50 anchor. Value 20 is halfway from 10 to 30, giving 75. Values outside the table range clamp to 0 and 100.
Example 3
boundaries = [-10,0,30]percentiles = [0,20,100]requests = [10,60,120]inverse = falsereturn = [-5,15,30]Percentile 10 lies halfway through the first percentile interval and maps to -5. Percentile 60 lies halfway from 20 to 100 and maps to 15. Percentile 120 is above the modeled range and clamps to the final value 30.
Constraints
2 <= boundaries.length <= 101boundaries.length == percentiles.length0 <= requests.length <= 100- Every boundary and request is between
-1000000and1000000, inclusive. - Every percentile is between
0and100, inclusive. - Both boundary and percentile arrays are strictly increasing.
- The first percentile is
0and the last is100. - All numbers are finite.