Reported September 2026
Two Sigmabinary search

Linear Interpolator

Reported by candidates from Two Sigma's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Two Sigma OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this Two Sigma OA, reported in September 2026, is treating the knots like clean, sorted, unique data. They aren't. The Linear Interpolator problem hands you up to 100000 points, possibly unsorted, possibly with duplicate x values, and asks for LI(x_input) with extrapolation on both ends. The core is sort plus binary search plus one line of slope math. The duplicate-x tie rule is where people lose test cases. If you blank on the edge handling mid-assessment, StealthCoder runs invisibly as a safety net and gives you the solution live.

The problem

You are given n knot points on a two-dimensional coordinate system.
Sort the points by their x-coordinates and connect adjacent points with straight lines. These segments define the piecewise-linear function LI(x). When x is outside the knot range, extrapolate using the two nearest knot points.
Complete linear_interpolate. Arrays x_knots and y_knots contain the coordinates, and the function must return LI(x_input).
For the sample, the knot points are (-2, 0), (-1, 10), (0, 15), (1, 0), and (2, 5). At x_input = -0.3, the result is 13.5.
If multiple knot points have the same x-coordinate x, use the smallest corresponding y when x_input <= x, and use the largest corresponding y when x_input > x. Apply the same tie rule during extrapolation.

Function
linear_interpolate(n: int, x_knots: float[], y_knots: float[], x_input: float) → float

Examples
Example 1
n = 5
x_knots = [-2, -1, 0, 1, 2]
y_knots = [0, 10, 15, 0, 5]
x_input = -0.3
return = 13.5
The input lies between -1 and 0, so use the segment from (-1, 10) to (0, 15).
Its slope is (15 - 10) / (0 - (-1)) = 5, so LI(-0.3) = 10 + 5 * 0.7 = 13.5. The runnable values were transcribed from the supplied source graph.

Constraints
1 < n <= 100000
x_knots and y_knots have the same length.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is simple. Pair the coordinates, sort by x, then binary search for the segment that contains x_input. Compute y1 + (y2 - y1) * (x - x1) / (x2 - x1). For extrapolation, reuse the first two points or the last two points. The pitfall is the duplicate rule. If several knots share an x, you need the smallest y when x_input <= x and the largest y when x_input > x. That means a vertical jump at a repeated x behaves like two different points depending on which side you're on. Collapse duplicates carefully, and don't divide by zero when x2 equals x1. Also watch the boundary: x_input equal to a knot should return that knot's smallest y. Sorting is O(n log n), and each query is O(log n). If the tie handling gets tangled live, StealthCoder can sketch the clean version for you, but know the rule cold before you go in.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Linear Interpolator 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Two Sigma's OA.

Two Sigma reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Linear Interpolator FAQ

How hard is the Two Sigma Linear Interpolator problem really?+

The math is easy, the edges are not. Sorting and a binary search are standard. The duplicate x tie rule and extrapolation are what separate a pass from a partial score. Expect most of your debugging time to go to those two cases, not the main formula.

What's the trick to the duplicate x-coordinates?+

For a repeated x, you effectively have two values. Use the smallest y when x_input <= x, and the largest y when x_input > x. So a vertical step is resolved by which side of x your query sits on. Group duplicates, track min and max y, and pick based on that comparison.

How do I handle x_input outside the knot range?+

Extend the line through the two nearest knots. Below the minimum, use the first two distinct x positions after sorting. Above the maximum, use the last two. Apply the same tie rule there, so the chosen y values at duplicated x are the correct min or max.

Do I need binary search or is a linear scan fine?+

With n up to 100000 and sorting already costing O(n log n), a single query scan is technically fine. Binary search is cleaner and safer if the function gets called repeatedly. Either works, but bisect-style logic is easy to write and shows you know the pattern.

How do I prepare for this in 48 hours?+

Write it once from scratch. Sort pairs, binary search the segment, compute the slope, then test the sample (-0.3 gives 13.5). Add tests for duplicates, x_input exactly on a knot, and both extrapolation directions. That covers nearly every failure mode this problem can throw.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Two Sigma.

OA at Two Sigma?
Invisible during screen share
Get it