Fastest Rolling One-Kilometer Average Speed from 5-Second Distance Samples
OptiverSoftware Engineer · Online AssessmentLast updated
Given the meters a runner covers in each 5-second interval, find the fastest average speed over a rolling one-kilometer stretch, defined as the shortest run of intervals ending at each point that covers at least 1000 meters. It tests sliding-window reasoning over large arrays and precise window definitions.
A runner's watch records how far the runner travels during each consecutive 5-second interval. You are given distances, where distances[i] is the number of meters covered during interval i. Find the fastest average speed the runner sustained over a rolling one-kilometer (1000-meter) stretch.
Function Signature
def fastest_rolling_km_speed(distances: list[int]) -> float:
Rules
- A window is a contiguous range of intervals
[l, r]withl <= r. Its distance isdistances[l] + ... + distances[r]meters and its duration is5 * (r - l + 1)seconds. - For each end interval
r, the rolling one-kilometer window ending atris the shortest window[l, r]whose distance is at least1000meters. If even[0, r]covers less than1000meters, intervalrhas no rolling window. - The average speed of a window is its full distance divided by its duration, in meters per second. The whole distance of the window counts even when it exceeds
1000meters; there is no interpolation inside an interval. - Return the maximum average speed over all rolling windows.
- If the total of all distances is less than
1000meters, no rolling window exists; return-1.0. - An answer within an absolute or relative error of
1e-6of the exact value is accepted.
Constraints
1 <= len(distances) <= 1000000 <= distances[i] <= 1000, and every value is an integer.- The sum of all distances is at most
100000000. - When a rolling window exists, the answer is greater than
0and at most200.0meters per second.
Examples
Example 1
- Input:
distances = [300, 400, 300, 200, 500, 400] - Output:
73.33333333333333 - Explanation: No window ending at interval 0 or 1 reaches 1000 meters. The rolling windows ending at intervals 2, 3, 4 and 5 are
[0, 2](1000 m in 15 s),[0, 3](1200 m in 20 s),[2, 4](1000 m in 15 s) and[3, 5](1100 m in 15 s), with average speeds of about 66.667, 60, 66.667 and 73.333 meters per second. The largest is 1100 / 15.
Example 2
- Input:
distances = [250, 250, 250, 240] - Output:
-1.0 - Explanation: The runner covers only 990 meters in total, so no window reaches one kilometer.
Example 3
- Input:
distances = [0, 600, 500, 0, 600] - Output:
110.0 - Explanation: The rolling window ending at interval 2 is
[1, 2]: 1100 meters in 10 seconds, or 110 meters per second. It does not include interval 0, because[1, 2]already reaches 1000 meters and is shorter. The rolling windows ending at intervals 3 and 4 are[1, 3]and[2, 4], each 1100 meters in 15 seconds.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.