Big-O in practice

Big-O says how the work grows as the input grows. n is the size of the input: the length of the list, the number of characters, the cells in a grid. Say what n is out loud in an interview, especially when there are two inputs (n and m).

To estimate it, count the loops. One pass over the data is O(n). A loop inside a loop over the same data is O(n²). Halving the search space each step is O(log n). Sorting is O(n log n). Constants drop out: two passes is still O(n).

To measure it, time the code at size n and again at 2n with time.perf_counter(). Linear code takes about twice as long. Quadratic code takes about four times as long. The tests in this chapter do exactly this with grows_like.

A common mistake: hiding a loop inside a "simple" call. x in some_list, list.index, list.remove and slicing are all O(n).

Run it, then double n again and watch the ratios.