Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Program Linear Interpolation
00:00
5 left

Program Linear Interpolation

MediumPython

Problem

Two Sigma research workflows may represent a signal as values sampled at irregular x-coordinates. Given these samples, evaluate the signal at multiple query coordinates using piecewise linear interpolation.

Implement linear_interpolate(x_values, y_values, queries). For each query q, find the two neighboring sample points (x_i, y_i) and (x_{i+1}, y_{i+1}) such that x_i <= q <= x_{i+1}, then return:

y_i + (q - x_i) * (y_{i+1} - y_i) / (x_{i+1} - x_i)

A query equal to a sample coordinate must return the corresponding sample value. Queries are guaranteed to lie within the range of the sample coordinates, so extrapolation is not required. Return results in the same order as queries.

Formal Specification

  • x_values: a strictly increasing list of n numbers.
  • y_values: a list of n numbers corresponding to x_values.
  • queries: a list of m numbers to evaluate.
  • Return a list of m floating-point numbers.

Constraints

  • 2 <= len(x_values) <= 10^5
  • len(x_values) == len(y_values)
  • 0 <= len(queries) <= 10^5
  • x_values is strictly increasing
  • Every query is between x_values[0] and x_values[-1]
  • All coordinates and values are finite numbers

Function Signature

def linear_interpolate(x_values, y_values, queries):
Interviewer

Your question is Program Linear Interpolation. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.