Engineering Craft · 16 min · free with an account

Complexity in Practice

Lesson preview · about 30%

Big-O tells you how something scales, not how fast it is.

Core idea

Complexity predicts growth; constants decide which one is faster today.

Start with the idea

A linear scan of a contiguous Vec often beats a logarithmic tree for small inputs because the constant factor is cache locality. The useful answer names the input size where the asymptotic term takes over, and mentions amortised cost: push on a Vec is O(1) amortised, but the reallocation that makes it so is O(n) and happens at the worst moment.

Big-O describes growth with input size, not wall-clock speed for one input. An O(n) scan over a small contiguous vector can beat an O(log n) tree search because of locality and simpler control flow. For large inputs, the growth rates can reverse that result. Explain the data size and operation mix before choosing. Also distinguish worst-case, average-case, and amortized analysis. Vec::push is amortized O(1), yet a particular push may reallocate and copy many elements.

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Rust Interview Prep