PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of linear interpolation, numerical reasoning with ordered 2D points, and input validation when computing values on a piecewise linear function.

  • medium
  • Two Sigma
  • Coding & Algorithms
  • Data Scientist

Evaluate a Piecewise Linear Function

Company: Two Sigma

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

You are given an ordered list of 2D points `[(x1, y1), (x2, y2), ..., (xn, yn)]`. Connecting each consecutive pair of points forms a piecewise linear function. Given a target value `target_x`, return the corresponding `y` value on the function at that x-coordinate. Assume: - The points are ordered by increasing `x`. - The function is defined by straight-line interpolation between consecutive points. - `target_x` may coincide with an existing point or fall between two adjacent points. Clarify how you would handle edge cases such as: - `target_x` outside the covered x-range - duplicate x-values - invalid input with fewer than 2 points Example: - Points: `[(1, 2), (3, 6), (5, 4)]` - `target_x = 2` - Output: `4`, because the point lies on the line segment between `(1, 2)` and `(3, 6)`.

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.

Return the interpolated y value for target_x on an ordered piecewise-linear function, or None for invalid/out-of-range inputs.

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

  1. Reject duplicate or decreasing x values.
  2. Find the segment containing target_x and apply linear interpolation.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Price-Time Order Matching - Two Sigma (medium)
  • Compute Piecewise Linear Interpolation - Two Sigma (medium)
  • Implement an In-Memory Database - Two Sigma (hard)
  • Merge two sorted linked lists - Two Sigma (hard)
  • Merge Two Sorted Lists - Two Sigma (hard)