KatInterview

Evaluate a Normalized CDF from Piecewise-Linear Density Samples

Math & Bit ManipulationAuto-graded · 17 test cases
MediumAuto-gradedMath & Bit Manipulationnumerical-integrationbinary-searchprefix-sums

Last updated

Build a cumulative distribution from sampled density points using exact integration of a piecewise-linear curve, then answer many query points with correct boundary behavior.

A continuous random variable has a density that is known only at a handful of sample points. You are given the sample locations xs, the density value at each location density, and a list of queries. For each query q, return the probability that the variable is at most q.

Model

  • Between two neighbouring sample locations, the density is the straight line joining the two sampled values (piecewise-linear interpolation).
  • Outside the interval [xs[0], xs[-1]], the density is zero. The variable therefore lives entirely on that interval.
  • The raw area under the interpolated curve is generally not 1, so divide every area by the total area under the whole curve. The returned values are probabilities of the normalized density.

Task

Implement a function that, for every query q, returns the normalized area under the interpolated density from xs[0] up to q:

  • If q <= xs[0], the answer is 0.0.
  • If q >= xs[-1], the answer is 1.0.
  • Otherwise, the answer is the exact area (not an approximation by sampling) of the interpolated curve from xs[0] to q, divided by the total area.

Results must be returned in the same order as queries. Because of floating-point rounding, clamp each result into [0.0, 1.0].

Input validation

Raise ValueError if any of these hold:

  • xs and density have different lengths, or fewer than 2 entries.
  • xs is not strictly increasing.
  • Any density value is negative.
  • The total area is not positive.

Constraints

  • 2 <= len(xs) <= 10^5
  • xs strictly increasing (valid inputs); density values are non-negative with positive total area.
  • 0 <= len(queries) <= 10^5; queries may be in any order and may repeat.
  • All numbers are finite floats or integers.
  • Answers are compared with a small floating-point tolerance.

Examples

Example 1

Input: density_to_cdf([0, 1, 2], [0, 1, 0], [-1, 0, 0.5, 1, 1.5, 2, 3])

Output: [0, 0, 0.125, 0.5, 0.875, 1, 1]

Example 2

Input: density_to_cdf([0, 2], [1, 1], [0, 0.5, 1, 2])

Output: [0, 0.25, 0.5, 1]

Example 3

Input: density_to_cdf([-2, -1, 0, 1], [0, 0, 1, 1], [-3, -1, -0.5, 0, 0.5, 1, 2])

Output: [0, 0, 0.08333333333333333, 0.3333333333333333, 0.6666666666666666, 1, 1]

Hints

Hint 1

Within one segment the density is linear, so the area from the segment's left end to a point at distance t is density_left * t + slope * t^2 / 2.

Hint 2

Precompute the cumulative area at every sample location (a prefix sum of trapezoid areas) so each query needs only one partial segment.

Hint 3

Use binary search on xs to find the segment containing each query, and handle queries at or beyond the ends before doing any interpolation.