Fastest Average Speed over a Rolling Kilometer
Reported by candidates from Optiver's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Optiver OA reported in September 2026 has a rolling-kilometer problem, and the first attempt usually fails the same way. People slide a window over whole intervals and forget that a kilometer can start and end mid-interval. The task is to find the fastest stretch covering exactly 1000 meters, which means the minimum time to cover 1000 meters, not the max distance in some fixed window. It's a two-pointer problem on prefix sums with fractional boundaries. If you blank on the geometry, StealthCoder runs invisibly during the live assessment as a safety net.
The problem
meters[i] is the distance traveled during the i-th consecutive five-second interval. Assume speed is constant within each interval. Among every continuous segment of the run covering exactly 1000 meters, return the maximum average speed in meters per second. A segment boundary may split an interval proportionally. Return -1.0 when the total distance is less than 1000 meters. Function fastestKilometerSpeed(meters: int[]) → double Examples Example 1 meters = [400,400,400] return = 80.0 At 80 meters per second throughout, exactly 1000 meters takes 12.5 seconds. Example 2 meters = [100,600,600,100] return = 120.0 The fastest kilometer starts and ends inside the two central intervals and takes 1000 / 120 seconds. Example 3 meters = [200,300,400] return = -1.0 The complete run covers only 900 meters. Constraints 1 <= meters.length <= 100000. 1 <= meters[i] <= 10000. The answer is accepted within 10^-6 absolute or relative error.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Think in time, not distance. Each interval is 5 seconds, so speed is meters[i]/5. You want the minimum time to cover exactly 1000 meters. Build prefix distance, then treat cumulative distance as a piecewise linear function of time. The optimal segment always has its start or its end on an interval boundary. So for each boundary, compute where 1000 meters later lands (forward) and where 1000 meters earlier lands (backward), using two pointers or binary search on the prefix array, then interpolate. Take the smallest duration, answer is 1000 divided by it. The pitfall is checking only whole-interval windows, which fails Example 2 where the best segment sits inside the middle two intervals. Also return -1.0 if the total is under 1000, and use doubles for the interpolation. StealthCoder is your hedge if the boundary argument slips under the clock.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Fastest Average Speed over a Rolling Kilometer cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Optiver's OA.
Optiver reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Fastest Average Speed over a Rolling Kilometer FAQ
What's the trick in the Optiver rolling kilometer problem?+
Convert to time. The best 1000 meter segment has at least one endpoint on an interval boundary, so you only test boundaries. For each one, interpolate where the segment's other end falls using prefix sums, compute elapsed time, and keep the minimum.
Why does a plain sliding window fail?+
Windows of whole intervals rarely sum to exactly 1000 meters. Segments can cut intervals proportionally, so the optimum often starts or ends mid-interval. Example 2 shows it: the best run lives inside the two 600 meter intervals, not on whole-interval boundaries.
How hard is this really?+
Medium. The idea is simple once you see the boundary argument, but the fractional interpolation is where bugs hide. Off-by-one on prefix indexes and integer division are the usual failures. Input size up to 100000 means you need O(n) or O(n log n).
What edge cases should I test?+
Total distance under 1000 returns -1.0. Total exactly 1000 returns 1000 divided by the full run time. Single interval inputs are covered by that. Also test a fast interval at the very start or end, since the backward scan matters there.
How do I prepare in 48 hours?+
Write prefix sums with two pointers on a fixed-sum window problem, then add fractional interpolation. Practice returning doubles and checking tolerance of 10^-6. Run Examples 1 to 3 by hand, especially the -1.0 case, before trusting your code.