Skip to the content

The fastest kilometer

Problem

"Which was my best kilometer?" Emma asks as she crosses the finish line. Her watch cannot tell her: all it logged was her total time each time she passed a kilometer marker.

Write a function that takes that list of running totals, in whole seconds and always increasing, and returns the number of the kilometer she ran in the least time. Kilometers are counted from 1, and the first one is measured from the start, which is second 0.

With [300, 590, 900, 1185] the kilometers took 300, 290, 310 and 285 seconds: you return 4.

If two kilometers tie, you return the first of them. If the list is empty, you return 0.

Examples

  • The example

    [300, 590, 900, 1185] → 4

  • The second kilometer

    [310, 600, 905] → 2

  • Two kilometers tied

    [300, 590, 880, 1180] → 2

  • A single kilometer

    [400] → 1

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def fastest_km(times):
    pass

JavaScript

function fastestKm(times) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →