Piecewise Linear Interpolation and Extrapolation
Reported by candidates from Two Sigma's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Two Sigma asked this in May 2026: build a piecewise linear interpolation function that handles both interpolation and extrapolation on a polyline. You're given unsorted 2D points, you sort them by x, connect them with line segments, then answer queries for y values at arbitrary x coordinates. This is a straightforward geometry problem with a binary search twist. StealthCoder will feed you the formula and the search logic if you blank on indexing.
The problem
Complete the function below. The function receives the full standard input as a single string and must return the exact standard output lines for the described problem. Original prompt Problem: Piecewise Linear Interpolation and Extrapolation for a Polyline You are given a set of 2D points [(x1,y1), (x2,y2),..., (xn,yn)]. After sorting points by increasing x, connect consecutive points with straight line segments to form a polyline. For each query xq, output the corresponding yq on this polyline: If xq lies between two adjacent points xi xn, extrapolate by extending the rightmost segment ((x_{n-1},y_{n-1})-(xn,yn)). If xq equals some xi, return yi exactly. Input (stdin) First line: two integers n q (#points, #queries) Next n lines: two real numbers x y Next q lines: one real number xq Output (stdout) For each query print one line yq (floating-point; 1e-6 error tolerated). Constraints 2 <= n <= 2e5 1 <= q <= 2e5 Points may be unsorted; sort them. After sorting, all x are strictly increasing. Target overall complexity: O((n+q) log n). Formula Given (xa,ya) and (xb,yb) (xa != xb): y(x) = ya + (yb - ya) * (x - xa) / (xb - xa) Function Description Complete solvePiecewiseLinearInterpolation. It has one parameter, String input, containing the full stdin payload. Return the stdout payload as an array of lines, without trailing newline characters. The returned string must match the expected standard output for the sample input. Use the limits and requirements stated in the prompt.Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick here is that the problem looks like math but is really about efficient lookup. Sort points by x in O(n log n). For each query, use binary search to find which segment the x value falls into, then apply the linear interpolation formula: y = ya + (yb - ya) * (x - xa) / (xb - xa). Handle three cases: x is left of all points (extrapolate using the leftmost segment), x is between two points (interpolate), x is right of all points (extrapolate using the rightmost segment). The O((n+q) log n) target is achieved by sorting once and binary searching once per query. Floating-point precision matters, so use the formula exactly as given.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Piecewise Linear Interpolation and Extrapolation cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Two Sigma's OA.
Two Sigma reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Piecewise Linear Interpolation and Extrapolation FAQ
What's the actual trick here?+
There isn't one. It's a straightforward binary search plus line interpolation. The trick is not overthinking it. Sort, binary search to find the containing segment, apply the formula. Most failures come from off-by-one errors in segment selection or forgetting to handle extrapolation.
Do I need to worry about floating-point precision?+
The problem says 1e-6 error is tolerated, so standard double precision is fine. Don't add epsilon checks unless your interpolation fails. Use the formula directly without simplifications.
What if two points have the same x value?+
The problem states that after sorting, all x values are strictly increasing. So you don't need to handle ties. Trust the constraint.
Is there a standard library function I should use?+
Most languages have a binary search utility. Use it. In Java, Collections.binarySearch or Arrays.binarySearch. In Python, bisect.bisect_left. Don't write your own unless you're confident.
How do I parse the input and format the output correctly?+
Read n and q, then n lines of x y pairs (as floats), then q lines of query x values. Output one y value per line, formatted as a floating-point number. Match the expected precision in the sample output.