22/32
Part 5 of 6 · The worked exampleLearn and do

Test the edges, then give the cost

Before you call the window done, run it on the inputs that break windows: an empty log, one page, and a repeat at the front.

print(shortest_covering_run([]))
print(shortest_covering_run(["home"]))
print(shortest_covering_run(["home", "home", "cart"]))
(0, 0)
(0, 1)
(1, 2)

The last one is (1, 2) because the window shrinks past the first home.

Each index enters once and leaves at most once: O(n) time, and O(m) memory for the m different pages. Here it is on logs where every request hits a different page, so the only answer is the whole log and starting over at every index does the most work:

nEvery startWindow
1,00021.5 ms0.15 ms
2,00090.2 ms0.31 ms
4,000363.9 ms0.63 ms

This is the same loop as before with its condition flipped. Shrink while broken to find the longest. Shrink while valid to find the shortest.

Write shortest_every_start, the obvious version you give first. The tests run shortest_covering_run beside it and compare the two on 500 random logs.