FastPrepAverage Speed Over Laps

Average Speed Over Laps

Jane Street logoJane Street● EasyFULLTIMEPHONE SCREEN

Updated

Problem statement

A car drives several laps, one after another. Lap i is lapLengths[i] miles long, and the car drives the whole lap at a constant speeds[i] miles per hour.

Return the car's average speed over the whole drive, in miles per hour: the total distance divided by the total time.

Answers within 10-6 of the correct value are accepted.

Function

averageSpeed(lapLengths: int[], speeds: int[]) → double

Examples

Example 1

lapLengths = [1,1]speeds = [60,90]return = 72.0

Lap 1 takes 1/60 of an hour and lap 2 takes 1/90 of an hour, 1/36 of an hour in total. Two miles in 1/36 of an hour is 72 miles per hour. It is not 75, because the car spends more time on the slower lap.

Example 2

lapLengths = [30,60]speeds = [30,60]return = 45.0

Both laps take one hour, so the car spends equal time at each speed. Total distance 90 miles over 2 hours gives 45, which here happens to be the plain average of the two speeds.

Example 3

lapLengths = [5]speeds = [40]return = 40.0

With one lap the average speed is that lap's speed.

Constraints

  • 1 <= lapLengths.length <= 104
  • speeds.length == lapLengths.length
  • 1 <= lapLengths[i] <= 1000
  • 1 <= speeds[i] <= 1000

More Jane Street Phone Screen questions

Similar questions at other companies

Interview reports that mention this question

Jane Street interview experiences

All Jane Street interview questionsSee Jane Street hiring insights
Saved
class Solution {
    public double averageSpeed(int[] lapLengths, int[] speeds) {
      // Write your code here.
    }
}
Checking account…