Waiting-Time Triangle Density: Normalize and Evaluate the CDF
Arrays & HashingAuto-graded · 18 test casesLast updated
Normalize a piecewise-linear triangular density to find its constant, then evaluate the resulting piecewise-quadratic cumulative distribution at a list of query points.
A continuous random variable X models how long (in hours) a customer waits for a pickup. Its density has a triangular shape, controlled by one positive constant c:
f(x) = c * xwhen0 <= x < 2f(x) = c * (4 - x)when2 <= x <= 4f(x) = 0for every otherx
Write a function that does two things:
- Finds the constant
cthat makesfa valid probability density, meaning the total area under it equals 1. - Evaluates the cumulative distribution function
F(t) = P(X <= t)at each query point in a given list.
Because the density is defined piecewise, F is also piecewise. It is 0 to the left of the support, 1 to the right of it, and it must stay continuous (in particular at t = 2, where the accumulated probability from the rising part must be carried into the falling part).
Input
queries: a list of real numbers, the points at which to evaluateF.
Output
Return a dictionary with two keys:
"k": the normalizing constantc, as a float."values": a list of floats, where thei-th entry isF(queries[i]), in the same order as the input.
Constraints
0 <= len(queries) <= 10^5- Each query is a finite real number (it may be negative, zero, or larger than 4).
- Queries may repeat and need not be sorted.
- Results are compared to the expected values with a small floating-point tolerance.
Examples
Example 1
Input: triangle_wait_cdf([-1, 0, 0.5, 1, 2, 3, 4, 5])
Output: {'k': 0.25, 'values': [0, 0, 0.03125, 0.125, 0.5, 0.875, 1, 1]}
Example 2
Input: triangle_wait_cdf([-10, 0, 2, 4, 10])
Output: {'k': 0.25, 'values': [0, 0, 0.5, 1, 1]}
Example 3
Input: triangle_wait_cdf([0, 1, 1.5, 2, 2.5, 3.5, 4])
Output: {'k': 0.25, 'values': [0, 0.125, 0.28125, 0.5, 0.71875, 0.96875, 1]}
Hints
Hint 1
The density is a triangle with base 4 and peak height 2c at x = 2. Set its area equal to 1 to get c.
Hint 2
For 0 <= t < 2, integrate c*s from 0 to t. For t >= 2, start from the mass already accumulated at t = 2 and add the integral of the falling part.
Hint 3
Handle the tails explicitly: F(t) = 0 for t < 0 and F(t) = 1 for t > 4. Check that both middle pieces give 1/2 at t = 2.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Finding the constant. The density rises linearly from 0 at x = 0 to height 2c at x = 2, then falls linearly back to 0 at x = 4. This is a triangle with base 4 and height 2c, so its area is (1/2) * 4 * 2c = 4c. Requiring the area to be 1 gives c = 1/4. Since c > 0, the density is non-negative everywhere, so it is valid.
Building the CDF. Use F(t) = ∫ f(s) ds from -∞ to t, handled region by region:
t < 0: no density lies to the left, soF(t) = 0.0 <= t < 2:F(t) = ∫₀ᵗ s/4 ds = t²/8.2 <= t <= 4: carry the accumulated massF(2) = 4/8 = 1/2and add the falling part:F(t) = 1/2 + ∫₂ᵗ (4 - s)/4 ds = t - t²/8 - 1. Equivalently,F(t) = 1 - (4 - t)²/8.t > 4: all the probability has been accumulated, soF(t) = 1.
Why it is correct. The pieces agree at the joints: at t = 0 both give 0, at t = 2 both give 1/2, and at t = 4 both give 1. Differentiating each piece returns the density, and each piece is non-decreasing, so this is a valid CDF. The main pitfall is forgetting the carried-over 1/2 on the second piece, which would make F drop at t = 2.
The function then simply evaluates this piecewise formula for each query in order and returns it along with c.
Complexity
Each query is answered in O(1), so the total time is O(n) for n queries, and the output list uses O(n) space.
Reference solution
from typing import List, Dict
def triangle_wait_cdf(queries: List[float]) -> Dict[str, object]:
# Area of the triangle is 4c, so c = 1/4.
c = 0.25
values = []
for t in queries:
if t < 0:
values.append(0.0)
elif t < 2:
values.append(t * t / 8.0)
elif t <= 4:
values.append(t - t * t / 8.0 - 1.0)
else:
values.append(1.0)
return {"k": c, "values": values}