Engineering Craft · 16 min · free lesson

Complexity in Practice

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

Core idea

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

Five common asymptotic growth rates compared as input size increases.
Complexity describes growth, not an exact clock time.

Understand it

A linear scan of a contiguous Vec can beat a logarithmic tree for small inputs because locality and constant costs matter. These growth classes are different. Measure a named workload to find any observed crossover; there is no universal input size. Distinguish amortised cost too: Vec::push is O(1) amortised, while an individual reallocation can move O(n) elements.

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.

Consider a service that looks up one item once in a list of 20. Building a hash map first may cost more than scanning. If it performs millions of lookups on 100,000 items, an index or map is more plausible. The break-even point depends on key size, hash cost, memory, cache behavior, and update frequency. Measure representative cases instead of inventing a universal threshold.

In an interview, state the dominant input dimension and count the operation that repeats. A nested loop is not always O(n²) if the inner pointer advances monotonically across the whole input. A recursive call can use additional stack space even when its time is linear. If you optimize an algorithm, mention memory and implementation complexity too. A useful answer gives both the asymptotic bound and a practical reason the chosen approach fits the stated constraints.

A complexity claim needs a model of the input. If you say O(n), specify whether n is items, bytes, vertices, or operations. For hash-based approaches, separate expected and worst-case behavior. For a recursive algorithm, count maximum call depth as space. These details let someone challenge the bound with a concrete counterexample.

Interview follow-up: Why might a small contiguous scan beat a structure with a better asymptotic bound? State n, cache locality and benchmark conditions.

Read the code

// O(n), one cache line at a time
items.iter().find(|item| item.id == id)

// O(log n), a pointer chase per level
tree.get(&id)

Linear and logarithmic growth are different complexity classes. Constants and memory locality still affect measured performance at a particular size.

Practice starter

fn main() {
    let mut values = Vec::new();
    for n in 0..100 { values.push(n); }
    assert_eq!(values.len(), 100);
}

Practise this code in Playground ↗

Try it

Compare a linear scan and an ordered-tree lookup for representative input sizes. Explain the different growth rates, then measure constants without claiming a universal crossover size.

What success looks like: Your answer mentions constants and cache behaviour, not only the exponent.

Check your understanding

Which statement describes repeated Vec::push with ordinary geometric growth assumptions?

Reveal the answer
  1. Every push has worst-case O(1) time regardless of capacity or allocator.
  2. Push is amortized O(1); a capacity-growing operation may relocate existing elements.
  3. Each push is O(log n) because Vec stores values in a balanced tree.

Amortized cost differs from one operation’s worst case. Reservation can avoid reallocations within the reserved capacity, not every future allocation.

Go deeper

Common misconception: Each push is O(log n) because Vec stores values in a balanced tree.

Vec is one contiguous buffer, not a tree. A push writes to the next slot in O(1) unless capacity runs out, when it reallocates and moves the elements.

  1. Identify the contract: Big-O tells you how something scales, not how fast it is.
  2. Trace the concrete example: Linear and logarithmic growth are different complexity classes. Constants and memory locality still affect measured performance at a particular size.
  3. Test the changed constraint: Why might a small contiguous scan beat a structure with a better asymptotic bound? State n, cache locality and benchmark conditions.

Transfer challenge: Why might a small contiguous scan beat a structure with a better asymptotic bound? State n, cache locality and benchmark conditions.

Connections

  • Caching and Invalidation: Staleness budgets and stampedes
  • Hashing and Collisions: Why Rust seeds its hasher
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Complexity in Practice (courseId "interview", lessonId "q-complexity")
Link: https://crabrix.com/learn/interview/q-complexity/
Goal: Complexity predicts growth; constants decide which one is faster today.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-complexity) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.