Back to problems

Evaluate piecewise linear function at x

Algorithm · Two Sigma · Medium

A sequence of points $$(x_0, y_0), (x_1, y_1), \ldots, (x_{n-1}, y_{n-1})$$ defines a polyline: join each consecutive pair by a straight line segment. The result is a piecewise linear function. For a target x-coordinate x, compute the corresponding function value y(x). If x matches an existing x-coordinate x_i, return y_i. When x falls strictly between two consecutive x-coordinates x_i and x_{i+1}, use linear interpolation on that segment: $$y(x) = y_i + (y_{i+1} - y_i)…

Checking your access…