Big-O: how work grows with n
Your system receives 1 million orders a day, and someone proposes comparing every order with every other one to find duplicates. Is that fine? Big-O notation answers this before you write any code. It describes how the number of basic operations grows with the input size , dropping constants and lower-order terms, so is .
Formally, means for some constant once is large enough. That is an upper bound. is the matching lower bound, and means both hold, so the growth rate is pinned exactly. In interviews people say "big-O" when they mean , and that is usually fine.
The classes you will meet, from fast to slow, are , , , , and . With :
Even at simple operations per second (compiled code; plain Python is closer to ), the all-pairs check takes seconds, close to three hours. Sorting by order ID and comparing neighbours is and finishes in well under a second.
To find the complexity, count loops. One pass over the data is . A loop inside a loop over the same data is . Halving the problem at each step, as binary search does, gives , because you can halve only about times. Also state the space complexity, the extra memory an algorithm uses, since most of the tricks below spend memory to save time.