Problem · Array

Speed to Pressure Lookup

Learn this problem
EasySpaceX logoSpaceXFULLTIMEPHONE SCREEN

Problem statement

Speed to Pressure Lookup

Write an algorithm that receives a speed and returns the corresponding pressure. Each speed interval uses the pressure associated with the smaller endpoint. For example, every speed from 0 up to, but not including, the next listed speed uses the pressure associated with 0.

The source notes that the lookup values are not guaranteed to remain hard-coded.

Practice Contract

For this exercise, assume speedBreakpoints is a strictly increasing list whose first value is 0, and pressures[i] is the pressure that begins at speedBreakpoints[i].

Return the pressure at the greatest breakpoint that is less than or equal to speed. A speed equal to a breakpoint uses that breakpoint's pressure. A speed above the final breakpoint uses the final pressure.

Function

lookupPressure(speedBreakpoints: int[], pressures: int[], speed: int) → int

Examples

Example 1

speedBreakpoints = [0,10,20,30]pressures = [100,95,80,60]speed = 7return = 100

The greatest breakpoint not exceeding 7 is 0, so the answer is pressures[0] = 100.

Example 2

speedBreakpoints = [0,10,20,30]pressures = [100,95,80,60]speed = 10return = 95

A speed equal to a breakpoint uses that breakpoint's pressure, so 10 maps to 95.

Example 3

speedBreakpoints = [0,10,20,30]pressures = [100,95,80,60]speed = 99return = 60

The speed is above the final breakpoint, so the final pressure applies.

Constraints

  • 1 ≤ speedBreakpoints.length == pressures.length ≤ 100,000
  • speedBreakpoints[0] == 0
  • 0 ≤ speedBreakpoints[i] ≤ 10^9
  • speedBreakpoints is strictly increasing.
  • -10^9 ≤ pressures[i] ≤ 10^9
  • 0 ≤ speed ≤ 10^9

More SpaceX problems

drafts saved locally
public int lookupPressure(int[] speedBreakpoints, int[] pressures, int speed) {
    // Write your code here.
}
speedBreakpoints[0,10,20,30]
pressures[100,95,80,60]
speed7
expected100
checking account