Fastest Kilometre Speed
Updated
Problem statement
Given distances, where distances[i] is the number of metres a runner covered in the i-th 5-second block of a run, return the fastest average speed, in metres per second, that the runner kept over any stretch of 1000 metres.
Inside a block the runner moves at a steady pace, so the moment the runner passes a point inside a block is found by linear interpolation. A stretch starts at a whole metre mark x and ends at x + 1000. Its time runs from the last moment the runner is at x to the first moment the runner reaches x + 1000, and its average speed is 1000 divided by that time.
One block may cover more than 1000 metres, and a block of 0 means the runner stood still. If the whole run is shorter than 1000 metres, return 0. Answers within 1% of the expected value are accepted.
Function
calculateFastestKilometreSpeed(distances: int[]) → doubleExamples
Example 1
distances = [250,300,250,200,400]return = 57.14286The best stretch is metre 400 to metre 1400. Metre 400 is halfway through the second block, passed at 7.5 seconds, and metre 1400 is reached at the end, at 25 seconds. That is 1000 / 17.5 = 57.14286 metres per second.
Example 2
distances = [1200]return = 240.0The run is one block of 1200 metres in 5 seconds, a steady 240 metres per second, so every 1000-metre stretch has that average speed.
Example 3
distances = [500,0,600]return = 71.42857The runner stands still for the whole second block. The best stretch is metre 100 to metre 1100: metre 100 is passed at 1 second and metre 1100 is reached at 15 seconds, so the speed is 1000 / 14 = 71.42857.
Example 4
distances = [300,300,300]return = 0.0The whole run is only 900 metres, so there is no 1000-metre stretch and the answer is 0.
Constraints
1 <= distances.length <= 10^40 <= distances[i] <= 5000- The sum of all values in
distancesis at most10^5.
More Bank of America interview questions
- Return Exact Cash Register ChangeOA · Seen Aug 2026
- Centered Star PyramidONSITE INTERVIEW · Seen Nov 2025
- Employees Without a ManagerONSITE INTERVIEW · Seen Nov 2025
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2025
- N-th Fibonacci NumberONSITE INTERVIEW · Seen Nov 2025
- Reverse Each Word in a SentenceONSITE INTERVIEW · Seen Nov 2025
- Second Highest SalaryONSITE INTERVIEW · Seen Nov 2025
- Sum Skipping Multiples of 7OA · Seen Nov 2025
- Wildcard Substring MatchOA · Seen Nov 2025
- Three Numbers That Add Up to a TargetONSITE INTERVIEW · Seen Apr 2025
Similar questions at other companies
- Find All Pythagorean TripletsDell Technologies · PHONE SCREENDell Technologies interview questions
- Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Fill Missing Points on Line SegmentsDatadog · PHONE SCREENDatadog interview questions
- Count Centaur Friend RequestsMeta · PHONE SCREENMeta interview questions
- Paint the CeilingWeRide · OAWeRide interview questions
- Paint the CeilingRoblox · OARoblox interview questions
- Fastest Average Speed over a Rolling KilometerOptiver · OAOptiver interview questions
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREENWhatNot interview questions
- Interval List IntersectionsGrammarly · PHONE SCREEN