Evaluate a Piecewise Linear Function
Company: Two Sigma
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates understanding of linear interpolation, numerical reasoning with ordered 2D points, and input validation when computing values on a piecewise linear function.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: ([(1,2),(3,6),(5,4)], 2)
Expected Output: 4.0
Explanation: Interpolate between the first two points.
Input: ([(0,0),(10,20)], 10)
Expected Output: 20.0
Explanation: A target equal to a known point returns that y value.
Input: ([(0,0),(2,4)], -1)
Expected Output: None
Explanation: Targets outside the covered range return None.
Input: ([(0,0),(0,2),(1,3)], 0)
Expected Output: None
Explanation: Non-increasing x values are treated as invalid input.
Hints
- Reject duplicate or decreasing x values.
- Find the segment containing target_x and apply linear interpolation.