Pressure Chamber Linear Interpolation
Problem statement
Pressure Chamber Linear Interpolation
A pressure chamber must follow target pressures as a vehicle's velocity changes during liftoff. The reported interview task provides known velocity-to-pressure points and asks for the target pressure at another velocity without introducing a sudden jump.
Practice Contract
For this exercise, assume velocities and pressures describe aligned known points. The velocities are strictly increasing, and queryVelocity lies between the first and last known velocities, inclusive.
If queryVelocity equals a known velocity, return its pressure. Otherwise, find the two neighboring known velocities and return the pressure obtained by linear interpolation between their aligned pressure values.
Function
interpolateChamberPressure(velocities: double[], pressures: double[], queryVelocity: double) → doubleExamples
Example 1
velocities = [0.0,100.0]pressures = [120.0,300.0]queryVelocity = 50.0return = 210.050 is halfway between velocities 0 and 100, so its pressure is halfway between 120 and 300: 210.
Example 2
velocities = [0.0,25.0,70.0]pressures = [100.0,150.0,330.0]queryVelocity = 25.0return = 150.0The query exactly matches the second known velocity, so return its aligned pressure 150.
Example 3
velocities = [0.0,20.0,50.0]pressures = [100.0,140.0,200.0]queryVelocity = 35.0return = 170.035 is halfway between 20 and 50, so interpolate halfway between pressures 140 and 200.
Constraints
2 ≤ velocities.length == pressures.length ≤ 100,000- All input values are finite doubles with absolute value at most
10^9. velocitiesis strictly increasing.velocities[0] ≤ queryVelocity ≤ velocities[velocities.length - 1]- Answers within
10^-6of the exact interpolated pressure are accepted.