Evaluate a Broken-Line Curve at a Query Position
Heaps, Sorting & SearchAuto-graded · 19 test casesLast updated
Sort scattered points by x, then find the y on the connecting line at a query x, interpolating inside the range and extending the end segments outside it.
You are given a list of 2D points, each written as [x, y]. The list is in arbitrary order, and no two points share the same x-value.
Imagine ordering the points by increasing x and joining each pair of neighbours with a straight segment. The result is a continuous broken-line curve (a polyline) defined over the x-axis.
Write a function that, given a query position query_x, returns the y-value of this curve at that position. The rules are:
- If
query_xequals the x-value of one of the points, return that point's y-value. - If
query_xlies strictly between the x-values of two neighbouring points (after ordering), return the y-value of the straight segment joining those two points atquery_x. - If
query_xis less than every x-value, extend the straight line through the two points with the smallest x-values and evaluate it atquery_x. - If
query_xis greater than every x-value, extend the straight line through the two points with the largest x-values and evaluate it atquery_x.
Use true (floating-point) division when computing slopes. Do not truncate to integers.
Constraints
- There are at least 2 points.
- All x-values among the points are distinct.
- Each point is a pair
[x, y]of numbers (integers or floats, possibly negative). query_xis a number and may lie inside or outside the range of the point x-values.- The input list should not be modified.
Examples
Example 1
Input: curve_value([[0, 0], [10, 20], [20, 10]], 5)
Output: 10
Hints
Hint 1
Once the points are ordered by x, the curve is fully determined by neighbouring pairs. Think about how to locate the pair that surrounds the query.
Hint 2
A binary search on the sorted x-values gives an insertion index. Check for an exact match first, then decide between left extension (index 0), right extension (index n), or a bracketing pair.
Hint 3
Interpolation and extrapolation share one formula: y1 + (q - x1) * (y2 - y1) / (x2 - x1). Only the choice of the two points differs.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The curve is defined by the points taken in increasing x order, so the first step is to sort a copy of the points by x. Then we build the list of sorted x-values and use binary search (bisect_left) to find the smallest index i with xs[i] >= query_x.
That index tells us which case we are in:
| Situation | Meaning | Points used |
|---|---|---|
i < n and xs[i] == query_x | Exact vertex | Return that point's y directly |
i == 0 (no exact match) | Query is left of all points | First two points |
i == n | Query is right of all points | Last two points |
| otherwise | xs[i-1] < query_x < xs[i] | Points i-1 and i |
For every non-exact case, the answer comes from the line through (x1, y1) and (x2, y2):
y = y1 + (query_x - x1) * (y2 - y1) / (x2 - x1)
Interpolation and extrapolation differ only in which two points are chosen.
Why it is correct
- Sorting reconstructs exactly the polyline described, and distinct x-values make the order unique.
bisect_leftreturns the first position whose x is at least the query. If it is not an exact match, then the previous x is strictly smaller, so the two neighbours bracket the query, which is the segment the polyline uses there.- Outside the range, the statement requires extending the end segments, which the first-two and last-two choices do.
- Because x-values are distinct,
x2 - x1is never zero, so the division is safe. Returning the stored y on exact matches avoids needless floating-point error. - The check
i < ncomes before readingxs[i]to prevent an out-of-range access.
Complexity
- Time: O(n log n) for sorting, plus O(log n) for the search.
- Space: O(n) for the sorted copy and the x-list.
For many queries, sort once and reuse the sorted data so each query costs O(log n).
Reference solution
from bisect import bisect_left
from typing import List
def curve_value(points: List[List[float]], query_x: float) -> float:
ordered = sorted(points, key=lambda p: p[0])
xs = [p[0] for p in ordered]
n = len(ordered)
i = bisect_left(xs, query_x)
# Exact hit on an existing vertex.
if i < n and xs[i] == query_x:
return ordered[i][1]
if i == 0:
left, right = ordered[0], ordered[1]
elif i == n:
left, right = ordered[-2], ordered[-1]
else:
left, right = ordered[i - 1], ordered[i]
x1, y1 = left
x2, y2 = right
return y1 + (query_x - x1) * (y2 - y1) / (x2 - x1)